在 make 命令行中构建多个目标

2024-03-22

假设我有一个 make 文件,并且有很多目标MyTarget1,MyTarget2,MyTarget3,...,MyTarget100.

如果我想使用 12 线程编译所有目标,我可以简单地使用make -j12 all.

现在我想编译所有目标的子集,假设MyTarget1, MyTarget2, MyTarget3, MyTarget4.

我知道逐一编译每个目标必须有效。这样就有12个线程在工作了MyTarget1,等等,继续工作MyTarget2,等等,...如果MyTarget并行度不高,比如helloworld这样的小目标,有些线程的时间就被浪费了。我不喜欢它的低并行性。

我想要一个高并行度的解决方案,比如make -j12 all,这12个线程可以在某个时刻处理不同的目标。

我怎样才能实现呢?

我想要类似的东西

make -j12 MyTarget1,MyTarget2,MyTarget3,MyTarget4

参考

按照已经给出的链接CMake的解决方案,现在我想知道它是否可以直接使用来实现make.

  • 如何使用 cmake --build 构建多个目标 https://stackoverflow.com/questions/47553569/how-can-i-build-multiple-targets-using-cmake-build

谢谢你的时间。


这是一个限制CMake。生成Makefile被明确列出为不并行运行。例如:

$ 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 are:

$ 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被列为非并行,它不允许同时构建多个目标。

$ 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

对于任意目标,您可以直接调用子 makefile 来成功:

$ 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。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 make 命令行中构建多个目标 的相关文章

随机推荐