并行调用所有子目录上的 gnumake (-j),然后才 运行 链接器规则最后(即重要顺序)
Call gnumake on all subdirs in parallel (-j) and only then run the linker-rule last (i.e. order important)
我有一个 C++ 生成文件项目。它非常适合非平行建筑。对于并行构建,它的工作效率为 99%...我遇到的唯一问题是我无法将最终的可执行文件 link 行放到最后 运行 (这一定是最后发生的事情)。
我有一些限制:我不想在我的 link 行上有任何 PHONY 依赖项,因为这会导致它每次都重新 link。 IE。一旦构建了我的目标,当我重新构建它时不应该重新linked。
这是(稍微做作的)最小示例。请不要试图挑剔它,它真的在这里只是为了显示问题,它不是真实的,但我正在显示的问题是。您应该能够 运行 这个并看到与我相同的问题。
# Set the default goal to build.
.DEFAULT_GOAL = build
#pretend subdirs (these don't really exist but it does not matter so long as they always try to be built)
MAKE_SUB_DIRS = 1 2 3
#pretend shared objects that are created by the pretend makefile sub directories (above)
OUTPUTS = out1.so out2.so out3.so
# Top level build goal - depends on all of the subdir makes and the target.out
.PHONY: build
build: $(MAKE_SUB_DIRS) target.out
@echo build finished
# Takes 1 second to build each of these pretend sub make directories. PHONY so always runs
.PHONY: $(MAKE_SUB_DIRS)
$(MAKE_SUB_DIRS):
@if [ ! -f out$@.so ] ; then echo making $@... ; sleep 1 ; echo a > out$@.so ; fi
# The main target, pretending that it needs out1,2 and 3 to link
# Should only run when target.out does not exist
# No PHONY deps allowed here
target.out:
@echo linking $@...
@ls $(OUTPUTS) > /dev/null
@cat $(OUTPUTS) > target.out
# Clean for convinience
clean:
@rm -rf *.so target.out
现在,我真的不关心 make
工作,我想要的是 make -j
工作。这是我试图 运行 它:
admin@osboxes:~/sandbox$ make clean
admin@osboxes:~/sandbox$
admin@osboxes:~/sandbox$ make -j - 1st attempt
making 1...
making 2...
linking target.out...
making 3...
ls: cannot access 'out1.so': No such file or directory
ls: cannot access 'out2.so': No such file or directory
ls: cannot access 'out3.so': No such file or directory
makefile:24: recipe for target 'target.out' failed
make: *** [target.out] Error 2
make: *** Waiting for unfinished jobs....
admin@osboxes:~/sandbox$
admin@osboxes:~/sandbox$ make -j - 2nd attempt
linking target.out...
build finished
admin@osboxes:~/sandbox$
admin@osboxes:~/sandbox$ make -j - 3rd attempt
build finished
admin@osboxes:~/sandbox$
所以我强调了我的三次尝试 运行 它。
- 尝试 1:您可以看到构建的所有 4 个依赖项同时启动(大约)。由于每个
makeing x...
都需要 1 秒,而 linking
几乎是即时的,所以我们看到了我的错误。但是所有三个 "libraries" 都正确构建。
- 尝试 2:库只有在它们不存在时才会被创建(那是 bash 代码——假装做 makefile 可能做的事情)。在这种情况下,它们已经创建。所以链接现在通过了,因为它只需要库存在。
- 尝试 3:没有任何反应,因为不需要:)
因此您可以看到所有步骤都在那里,只需对它们进行排序即可。我希望 make sub dirs 1, 2, 3
以任何顺序并行构建,然后只有在它们全部完成后我才希望 target.out
到 运行 (即 linker ).
虽然我不想这样称呼它:$(MAKE) target.out
因为在我真正的 makefile 中我有很多变量都设置...
我尝试查看(来自其他答案).NOT_PARALLEL
并使用 dep order 运算符 |
(管道),并且我尝试订购大量规则以获得 target.out 最后....但是 -j
选项只是通过所有这些并破坏了我的订购:( ...必须有一些简单的方法来做到这一点?
好的,所以我找到了 "a" 解决方案...但它有点违背我的要求,因此很丑陋(但不是 那个 丑陋):
我可以理解的唯一确保并行构建顺序的方法(同样来自我阅读的其他答案)是这样的:
rule: un ordered deps
rule:
@echo this will happen last
这里将以任意顺序生成(或生成?)三个 dep,最后回显行将是 运行。
然而,我想做的事情是一个规则,特别是这样,它检查是否有任何更改或文件是否不存在 - 然后,只有这样,运行s规则。
我所知道的 运行 从另一条规则的边界到一条规则的唯一方法是递归地调用它的 make 。但是,我在同一个 makefile 上递归调用 make 时遇到以下问题:
- 默认不传入变量
- 许多相同的规则将被重新定义(不允许或不需要)
所以我想到了这个:
生成文件:
# Set the default goal to build.
.DEFAULT_GOAL = build
#pretend subdirs (these don't really exist but it does not matter so long as they always try to be built)
MAKE_SUB_DIRS = 1 2 3
#pretend shared objects that are created by the pretend makefile sub directories (above)
OUTPUTS = out1.so out2.so out3.so
# Top level build goal - depends on all of the subdir makes and the target.out
export OUTPUTS
.PHONY: build
build: $(MAKE_SUB_DIRS)
@$(MAKE) -f link.mk target.out --no-print-directory
@echo build finished
# Takes 1 second to build each of these pretend sub make directories. PHONY so always runs
.PHONY: $(MAKE_SUB_DIRS)
$(MAKE_SUB_DIRS):
@if [ ! -f out$@.so ] ; then echo making $@... ; sleep 1 ; echo a > out$@.so ; fi
# Clean for convinience
clean:
@rm -rf *.so target.out
link.mk:
# The main target, pretending that it needs out1,2 and 3 to link
# Should only run when target.out does not exist
# No PHONY deps allowed here
target.out:
@echo linking $@...
@ls $(OUTPUTS) > /dev/null
@cat $(OUTPUTS) > target.out
所以在这里我将链接器规则放入一个名为 link.mk 的单独 makefile 中,这避免了对同一文件的递归 make 调用(因此使用重新定义的规则)。但是我必须导出我需要传递的所有变量...这很丑陋并且如果这些变量发生变化会增加一些维护开销。
...但是...有效:)
我不会很快标记这个,因为我希望一些天才会指出一个 neater/better 方法来做到这一点...
通常您只需将 lib 文件添加为 target.out:
的先决条件
target.out: $(OUTPUTS)
@echo linking $@...
事实是,如果任何输出 lib 文件更新,这将重新链接 target.out。通常这是你想要的(如果 lib 已经改变,你需要重新链接目标),但你明确地说你没有。
GNU make 提供了一个名为 "order only prerequisites" 的扩展,您可以将其放在 |
:
之后
target.out: | $(OUTPUTS)
@echo linking $@...
现在,target.out只有在不存在的情况下才会重新链接,但那样的话,它仍然会等到$(OUTPUTS)
构建完成后
如果您的 $(OUTPUT)
文件是由子目录生成的,您可能会发现您需要这样的规则:
.PHONY: $(OUTPUT)
$(OUTPUT):
$(MAKE) -C $$(dirname $@) $@
调用递归 make,除非您有其他规则将在子目录中调用 make
编辑:添加将变量传递给子品牌的方法示例。通过将 $(SUBDIRS)
添加到 build
的先决条件而不是在其配方中进行优化。
我不确定我是否完全理解您的组织,但处理子目录的一种解决方案如下。我假设,有点像您的示例,构建子目录 foo
在顶级目录中生成 foo.o
。我还假设您的顶级 Makefile 定义了您在构建子目录时要传递给子品牌的变量(VAR1
、VAR2
...)。
VAR1 := some-value
VAR2 := some-other-value
...
SUBDIRS := foo bar baz
SUBOBJS := $(patsubst %,%.o,$(SUBDIRS))
.PHONY: build clean $(SUBDIRS)
build: $(SUBDIRS)
$(MAKE) top
$(SUBDIRS):
$(MAKE) -C $@ VAR1=$(VAR1) VAR2=$(VAR2) ...
top: top.o $(SUBOBJS)
$(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)
top.o: top.cc
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
rm -f top top.o $(SUBOBJS)
for d in $(SUBDIRS); do $(MAKE) -C $$d clean; done
这是并行安全的,并保证 link 只有在所有子构建完成后才会发生。请注意,您还可以 export
将要传递给子 make 的变量,而不是在命令行中传递它们:
VAR1 := some-value
VAR2 := some-other-value
...
export VAR1 VAR2 ...
我有一个 C++ 生成文件项目。它非常适合非平行建筑。对于并行构建,它的工作效率为 99%...我遇到的唯一问题是我无法将最终的可执行文件 link 行放到最后 运行 (这一定是最后发生的事情)。
我有一些限制:我不想在我的 link 行上有任何 PHONY 依赖项,因为这会导致它每次都重新 link。 IE。一旦构建了我的目标,当我重新构建它时不应该重新linked。
这是(稍微做作的)最小示例。请不要试图挑剔它,它真的在这里只是为了显示问题,它不是真实的,但我正在显示的问题是。您应该能够 运行 这个并看到与我相同的问题。
# Set the default goal to build.
.DEFAULT_GOAL = build
#pretend subdirs (these don't really exist but it does not matter so long as they always try to be built)
MAKE_SUB_DIRS = 1 2 3
#pretend shared objects that are created by the pretend makefile sub directories (above)
OUTPUTS = out1.so out2.so out3.so
# Top level build goal - depends on all of the subdir makes and the target.out
.PHONY: build
build: $(MAKE_SUB_DIRS) target.out
@echo build finished
# Takes 1 second to build each of these pretend sub make directories. PHONY so always runs
.PHONY: $(MAKE_SUB_DIRS)
$(MAKE_SUB_DIRS):
@if [ ! -f out$@.so ] ; then echo making $@... ; sleep 1 ; echo a > out$@.so ; fi
# The main target, pretending that it needs out1,2 and 3 to link
# Should only run when target.out does not exist
# No PHONY deps allowed here
target.out:
@echo linking $@...
@ls $(OUTPUTS) > /dev/null
@cat $(OUTPUTS) > target.out
# Clean for convinience
clean:
@rm -rf *.so target.out
现在,我真的不关心 make
工作,我想要的是 make -j
工作。这是我试图 运行 它:
admin@osboxes:~/sandbox$ make clean admin@osboxes:~/sandbox$ admin@osboxes:~/sandbox$ make -j - 1st attempt making 1... making 2... linking target.out... making 3... ls: cannot access 'out1.so': No such file or directory ls: cannot access 'out2.so': No such file or directory ls: cannot access 'out3.so': No such file or directory makefile:24: recipe for target 'target.out' failed make: *** [target.out] Error 2 make: *** Waiting for unfinished jobs.... admin@osboxes:~/sandbox$ admin@osboxes:~/sandbox$ make -j - 2nd attempt linking target.out... build finished admin@osboxes:~/sandbox$ admin@osboxes:~/sandbox$ make -j - 3rd attempt build finished admin@osboxes:~/sandbox$
所以我强调了我的三次尝试 运行 它。
- 尝试 1:您可以看到构建的所有 4 个依赖项同时启动(大约)。由于每个
makeing x...
都需要 1 秒,而linking
几乎是即时的,所以我们看到了我的错误。但是所有三个 "libraries" 都正确构建。 - 尝试 2:库只有在它们不存在时才会被创建(那是 bash 代码——假装做 makefile 可能做的事情)。在这种情况下,它们已经创建。所以链接现在通过了,因为它只需要库存在。
- 尝试 3:没有任何反应,因为不需要:)
因此您可以看到所有步骤都在那里,只需对它们进行排序即可。我希望 make sub dirs 1, 2, 3
以任何顺序并行构建,然后只有在它们全部完成后我才希望 target.out
到 运行 (即 linker ).
虽然我不想这样称呼它:$(MAKE) target.out
因为在我真正的 makefile 中我有很多变量都设置...
我尝试查看(来自其他答案).NOT_PARALLEL
并使用 dep order 运算符 |
(管道),并且我尝试订购大量规则以获得 target.out 最后....但是 -j
选项只是通过所有这些并破坏了我的订购:( ...必须有一些简单的方法来做到这一点?
好的,所以我找到了 "a" 解决方案...但它有点违背我的要求,因此很丑陋(但不是 那个 丑陋):
我可以理解的唯一确保并行构建顺序的方法(同样来自我阅读的其他答案)是这样的:
rule: un ordered deps
rule:
@echo this will happen last
这里将以任意顺序生成(或生成?)三个 dep,最后回显行将是 运行。
然而,我想做的事情是一个规则,特别是这样,它检查是否有任何更改或文件是否不存在 - 然后,只有这样,运行s规则。
我所知道的 运行 从另一条规则的边界到一条规则的唯一方法是递归地调用它的 make 。但是,我在同一个 makefile 上递归调用 make 时遇到以下问题:
- 默认不传入变量
- 许多相同的规则将被重新定义(不允许或不需要)
所以我想到了这个:
生成文件:
# Set the default goal to build.
.DEFAULT_GOAL = build
#pretend subdirs (these don't really exist but it does not matter so long as they always try to be built)
MAKE_SUB_DIRS = 1 2 3
#pretend shared objects that are created by the pretend makefile sub directories (above)
OUTPUTS = out1.so out2.so out3.so
# Top level build goal - depends on all of the subdir makes and the target.out
export OUTPUTS
.PHONY: build
build: $(MAKE_SUB_DIRS)
@$(MAKE) -f link.mk target.out --no-print-directory
@echo build finished
# Takes 1 second to build each of these pretend sub make directories. PHONY so always runs
.PHONY: $(MAKE_SUB_DIRS)
$(MAKE_SUB_DIRS):
@if [ ! -f out$@.so ] ; then echo making $@... ; sleep 1 ; echo a > out$@.so ; fi
# Clean for convinience
clean:
@rm -rf *.so target.out
link.mk:
# The main target, pretending that it needs out1,2 and 3 to link
# Should only run when target.out does not exist
# No PHONY deps allowed here
target.out:
@echo linking $@...
@ls $(OUTPUTS) > /dev/null
@cat $(OUTPUTS) > target.out
所以在这里我将链接器规则放入一个名为 link.mk 的单独 makefile 中,这避免了对同一文件的递归 make 调用(因此使用重新定义的规则)。但是我必须导出我需要传递的所有变量...这很丑陋并且如果这些变量发生变化会增加一些维护开销。
...但是...有效:)
我不会很快标记这个,因为我希望一些天才会指出一个 neater/better 方法来做到这一点...
通常您只需将 lib 文件添加为 target.out:
的先决条件target.out: $(OUTPUTS)
@echo linking $@...
事实是,如果任何输出 lib 文件更新,这将重新链接 target.out。通常这是你想要的(如果 lib 已经改变,你需要重新链接目标),但你明确地说你没有。
GNU make 提供了一个名为 "order only prerequisites" 的扩展,您可以将其放在 |
:
target.out: | $(OUTPUTS)
@echo linking $@...
现在,target.out只有在不存在的情况下才会重新链接,但那样的话,它仍然会等到$(OUTPUTS)
构建完成后
如果您的 $(OUTPUT)
文件是由子目录生成的,您可能会发现您需要这样的规则:
.PHONY: $(OUTPUT)
$(OUTPUT):
$(MAKE) -C $$(dirname $@) $@
调用递归 make,除非您有其他规则将在子目录中调用 make
编辑:添加将变量传递给子品牌的方法示例。通过将 $(SUBDIRS)
添加到 build
的先决条件而不是在其配方中进行优化。
我不确定我是否完全理解您的组织,但处理子目录的一种解决方案如下。我假设,有点像您的示例,构建子目录 foo
在顶级目录中生成 foo.o
。我还假设您的顶级 Makefile 定义了您在构建子目录时要传递给子品牌的变量(VAR1
、VAR2
...)。
VAR1 := some-value
VAR2 := some-other-value
...
SUBDIRS := foo bar baz
SUBOBJS := $(patsubst %,%.o,$(SUBDIRS))
.PHONY: build clean $(SUBDIRS)
build: $(SUBDIRS)
$(MAKE) top
$(SUBDIRS):
$(MAKE) -C $@ VAR1=$(VAR1) VAR2=$(VAR2) ...
top: top.o $(SUBOBJS)
$(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)
top.o: top.cc
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
rm -f top top.o $(SUBOBJS)
for d in $(SUBDIRS); do $(MAKE) -C $$d clean; done
这是并行安全的,并保证 link 只有在所有子构建完成后才会发生。请注意,您还可以 export
将要传递给子 make 的变量,而不是在命令行中传递它们:
VAR1 := some-value
VAR2 := some-other-value
...
export VAR1 VAR2 ...