Xu Hui
Xu Hui

Reputation: 1265

build multiple targets in make command line

Suppose I have a make file, and I have many target MyTarget1,MyTarget2,MyTarget3,...,MyTarget100.

If I want compile all targets using 12 thread, I can simply using make -j12 all.

Now I want compile a subset of all target, suppose MyTarget1, MyTarget2, MyTarget3, MyTarget4.

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.

I want a high parallism solution, like make -j12 all, which 12 thread can work on different target at a certain moment.

How can I implement that?

I want something like

make -j12 MyTarget1,MyTarget2,MyTarget3,MyTarget4

Reference

follow link already given the CMake's solution, now I am wondering can it be implement directly using make.

Thanks for your time.

Upvotes: 4

Views: 1582

Answers (1)

raspy
raspy

Reputation: 4261

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
  )

Relevant parts of generated 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

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

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 though.

Upvotes: 4

Related Questions