Takatoshi Kondo
Takatoshi Kondo

Reputation: 3550

How to write specific multiple target parallel build-able CMakeLists.txt?

This is related question of How to build specific multiple targets parallelly using make?

Multiple targets Makefile can parallel build with specific multiple target. For example, I have target a, b, and c.

I can do as follows:

make -j2 a b

The targets a and b parallelly.

However my Makefile that is generated by cmake and CMakeList.txt doesn't work parallelly.

Here is my CMakeLists.txt

CMAKE_MINIMUM_REQUIRED (VERSION 3.13.0)
PROJECT (para)

ADD_EXECUTABLE (a a.cpp)
ADD_EXECUTABLE (b b.cpp)
ADD_EXECUTABLE (c c.cpp)

a.cpp, b.cpp, and c.cpp has the same contents

int main() {}

I create build directory as follows:

mkdir build

Then execute cmake as follows:

cd buid
cmake ..

Then execute make with -j2 option no specific target.

make -j2
[master][~/work/training/code/cmake/parallel/build]
[ 16%] Building CXX object CMakeFiles/b.dir/b.cpp.o
[ 33%] Building CXX object CMakeFiles/c.dir/c.cpp.o
[ 50%] Linking CXX executable b
[ 66%] Linking CXX executable c
[ 66%] Built target c
[ 66%] Built target b
[ 83%] Building CXX object CMakeFiles/a.dir/a.cpp.o
[100%] Linking CXX executable a
[100%] Built target a

It is expected result. Two targets build parallelly.

However, if I pass specific targets as follows, build becomes sequentially.

make -j2 a b
[ 50%] Building CXX object CMakeFiles/a.dir/a.cpp.o
[100%] Linking CXX executable a
[100%] Built target a
[ 50%] Building CXX object CMakeFiles/b.dir/b.cpp.o
[100%] Linking CXX executable b
[100%] Built target b

How should I write CMakeLists.txt to build parallelly with specific targets ?

Here is generated Makefile by cmake and CMakeLists.txt above.

;; This buffer is for text that is not saved, and for Lisp evaluation.
;; To create a file, visit it with C-x C-f and enter text in its buffer.

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.18

# Default target executed when no arguments are given to make.
default_target: all

.PHONY : default_target

# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:


#=============================================================================
# Special targets provided by cmake.

# Disable implicit rules so canonical targets will work.
.SUFFIXES:


# Disable VCS-based implicit rules.
% : %,v


# Disable VCS-based implicit rules.
% : RCS/%


# Disable VCS-based implicit rules.
% : RCS/%,v


# Disable VCS-based implicit rules.
% : SCCS/s.%


# Disable VCS-based implicit rules.
% : s.%


.SUFFIXES: .hpux_make_needs_suffix_list


# Command-line flag to silence nested $(MAKE).
$(VERBOSE)MAKESILENT = -s

#Suppress display of executed commands.
$(VERBOSE).SILENT:

# A target that is always out of date.
cmake_force:

.PHONY : cmake_force

#=============================================================================
# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake

# The command to remove a file.
RM = /usr/bin/cmake -E rm -f

# Escaping for special characters.
EQUALS = =

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/kondo/work/training/code/cmake/parallel

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/kondo/work/training/code/cmake/parallel/build

#=============================================================================
# Targets provided globally by CMake.

# Special rule for the target rebuild_cache
rebuild_cache:
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
    /usr/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache

.PHONY : rebuild_cache/fast

# Special rule for the target edit_cache
edit_cache:
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
    /usr/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache

.PHONY : edit_cache/fast

# The main all target
all: cmake_check_build_system
    $(CMAKE_COMMAND) -E cmake_progress_start /home/kondo/work/training/code/cmake/parallel/build/CMakeFiles /home/kondo/work/training/code/cmake/parallel/build//CMakeFiles/progress.marks
    $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
    $(CMAKE_COMMAND) -E cmake_progress_start /home/kondo/work/training/code/cmake/parallel/build/CMakeFiles 0
.PHONY : all

# The main clean target
clean:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean
.PHONY : clean

# The main clean target
clean/fast: clean

.PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
    $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall

# Prepare targets for installation.
preinstall/fast:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast

# clear depends
depend:
    $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend

#=============================================================================
# Target rules for targets named c

# Build rule for target.
c : cmake_check_build_system
    $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 c
.PHONY : c

# fast build rule for target.
c/fast:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/c.dir/build.make CMakeFiles/c.dir/build
.PHONY : c/fast

#=============================================================================
# Target rules for targets named b

# Build rule for target.
b : cmake_check_build_system
    $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 b
.PHONY : b

# fast build rule for target.
b/fast:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/build
.PHONY : b/fast

#=============================================================================
# Target rules for targets named a

# Build rule for target.
a : cmake_check_build_system
    $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 a
.PHONY : a

# fast build rule for target.
a/fast:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/a.dir/build.make CMakeFiles/a.dir/build
.PHONY : a/fast

a.o: a.cpp.o

.PHONY : a.o

# target to build an object file
a.cpp.o:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/a.dir/build.make CMakeFiles/a.dir/a.cpp.o
.PHONY : a.cpp.o

a.i: a.cpp.i

.PHONY : a.i

# target to preprocess a source file
a.cpp.i:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/a.dir/build.make CMakeFiles/a.dir/a.cpp.i
.PHONY : a.cpp.i

a.s: a.cpp.s

.PHONY : a.s

# target to generate assembly for a file
a.cpp.s:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/a.dir/build.make CMakeFiles/a.dir/a.cpp.s
.PHONY : a.cpp.s

b.o: b.cpp.o

.PHONY : b.o

# target to build an object file
b.cpp.o:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/b.cpp.o
.PHONY : b.cpp.o

b.i: b.cpp.i

.PHONY : b.i

# target to preprocess a source file
b.cpp.i:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/b.cpp.i
.PHONY : b.cpp.i

b.s: b.cpp.s

.PHONY : b.s

# target to generate assembly for a file
b.cpp.s:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/b.cpp.s
.PHONY : b.cpp.s

c.o: c.cpp.o

.PHONY : c.o

# target to build an object file
c.cpp.o:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/c.dir/build.make CMakeFiles/c.dir/c.cpp.o
.PHONY : c.cpp.o

c.i: c.cpp.i

.PHONY : c.i

# target to preprocess a source file
c.cpp.i:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/c.dir/build.make CMakeFiles/c.dir/c.cpp.i
.PHONY : c.cpp.i

c.s: c.cpp.s

.PHONY : c.s

# target to generate assembly for a file
c.cpp.s:
    $(MAKE) $(MAKESILENT) -f CMakeFiles/c.dir/build.make CMakeFiles/c.dir/c.cpp.s
.PHONY : c.cpp.s

# Help Target
help:
    @echo "The following are some of the valid targets for this Makefile:"
    @echo "... all (the default if no target is provided)"
    @echo "... clean"
    @echo "... depend"
    @echo "... edit_cache"
    @echo "... rebuild_cache"
    @echo "... a"
    @echo "... b"
    @echo "... c"
    @echo "... a.o"
    @echo "... a.i"
    @echo "... a.s"
    @echo "... b.o"
    @echo "... b.i"
    @echo "... b.s"
    @echo "... c.o"
    @echo "... c.i"
    @echo "... c.s"
.PHONY : help



#=============================================================================
# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
    $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

Upvotes: 1

Views: 2076

Answers (1)

Takatoshi Kondo
Takatoshi Kondo

Reputation: 3550

The top level Makefile that is generated by cmake has .NOPARALLEL:. It prevents parallel build. So even if I execute make -j2 a b, target a and b build sequentially.

cmake also generates CMakeFiles/Makefile2. It is used as follows from the top level Makefile:

# The main all target
all: cmake_check_build_system
    $(CMAKE_COMMAND) -E cmake_progress_start /home/kondo/work/training/code/cmake/parallel/build/CMakeFiles /home/kondo/work/training/code/cmake/parallel/build//CMakeFiles/progress.marks
    $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
    $(CMAKE_COMMAND) -E cmake_progress_start /home/kondo/work/training/code/cmake/parallel/build/CMakeFiles 0
.PHONY : all

CMakeFiles/Makefile2 doesn't prevent parallel build.

So I can parallel build the specific targets if I use explicitly the CMakeFiles/Makefile2.

The command is here.

make -f CMakeFiles/Makefile2 -j2 a b

Thank you @Tsyvarev , your comments are very helpful.

Upvotes: 4

Related Questions