在make命令行中构建多个目标 [英] build multiple targets in make command line
问题描述
假设我有一个生成文件,并且我有许多目标 MyTarget1,MyTarget2,MyTarget3,...,MyTarget100
.
Suppose I have a make file, and I have many target MyTarget1,MyTarget2,MyTarget3,...,MyTarget100
.
如果我想使用12个线程编译所有目标,则可以简单地使用 make -j12 all
.
If I want compile all targets using 12 thread, I can simply using make -j12 all
.
现在,我想编译所有目标的子集,假设 MyTarget1,MyTarget2,MyTarget3,MyTarget4
.
Now I want compile a subset of all target, suppose MyTarget1, MyTarget2, MyTarget3, MyTarget4
.
我知道必须逐个编译每个目标.这样,在 MyTarget1
上有12个线程工作,等待,在 MyTarget2
上工作,等等,...如果 MyTarget
的并行度不高,比如像helloworld这样的小目标,浪费了一些线程的时间.我不喜欢它的低平行度.
I know that compile each target one by one must work. In this way, 12 thread work on MyTarget1
, wait, work on MyTarget2
, wait, ... If MyTarget
do not have a high parallism, for example it is a small target like helloworld, some thread's time is wasted. I do not like it for its low parallism.
我想要一个高度并行的解决方案,例如 make -j12 all
,这12个线程可以在某个时刻在不同的目标上工作.
I want a high parallism solution, like make -j12 all
, which 12 thread can work on different target at a certain moment.
我该如何实现?
我想要类似的东西
make -j12 MyTarget1,MyTarget2,MyTarget3,MyTarget4
参考
跟随链接已经给出了 CMake
的解决方案,现在我想知道是否可以使用 make
直接实现.
Reference
follow link already given the CMake
's solution, now I am wondering can it be implement directly using make
.
感谢您的时间.
推荐答案
这是 CMake
的限制.明确列出了生成的 Makefile
,使其不能并行运行.例如:
This is a limitation of CMake
. Generated Makefile
is explicitly listed to not run in parallel. For example:
$ cat CMakeLists.txt
project(foo C)
add_custom_target(target1 ALL
COMMAND python3 -c "import time; time.sleep(5)"
VERBATIM
)
add_custom_target(target2 ALL
COMMAND python3 -c "import time; time.sleep(5)"
VERBATIM
)
生成的 Makefile
的相关部分是:
$ cat Makefile
...
# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:
...
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/raspy/so-62013595/CMakeFiles /home/raspy/so-62013595/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/raspy/so-62013595/CMakeFiles 0
.PHONY : all
...
# Build rule for target.
target2: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 target2
.PHONY : target2
...
# Build rule for target.
target1: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 target1
.PHONY : target1
因此,您可以看到每个目标都传播到一个子makefile,但是由于列出的顶部 Makefile
不是并行的,因此不允许同时构建多个目标./p>
So as you can see every target is propagated to a sub-makefile, but since this top Makefile
is listed as not parallel, it will not allow to build multiple targets at the same time.
$ make -j8 target1 target2 | ts
May 26 15:45:06 Built target target1
May 26 15:45:13 Built target target2 # <--- Built after target1 completed
对于任意目标,直接调用sub-makefile可能会成功:
For arbitrary targets, you might be successful with calling sub-makefile directly:
$ make -j8 -f CMakeFiles/Makefile2 target1 target2 | ts
May 26 15:45:42 Built target target2
May 26 15:45:42 Built target target1 # <--- Built simultaneously with target2
YMMV.
这篇关于在make命令行中构建多个目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!