如何编写特定的多目标并行可构建CMakeLists.txt?

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

这是

的相关问题

多目标 Makefile 可以并行构建特定的多目标。 比如我有目标a,b,c.

我可以这样做:

make -j2 a b

目标a和b并行。

但是我的由 cmake 和 CMakeList.txt 生成的 Makefile 不能并行工作。

这是我的 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、c.cpp内容相同

int main() {}

我创建构建目录如下:

mkdir build

然后执行cmake如下:

cd buid
cmake ..

然后使用 -j2 选项执行 make,没有特定的目标。

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

这是预期的结果。两个目标并行构建。

但是,如果我按如下方式传递特定的目标,构建就会变得顺序。

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

我应该如何编写 CMakeLists.txt 以与特定目标并行构建?

这里是cmake和上面CMakeLists.txt生成的Makefile

;; 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

cmake生成的顶层Makefile.NOPARALLEL:。它阻止并行构建。 所以即使我执行 make -j2 a b,目标 a 和 b 按顺序构建。

cmake 也会生成 CMakeFiles/Makefile2。从顶层开始使用如下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 不会阻止并行构建。

因此,如果我明确使用 CMakeFiles/Makefile2,我可以并行构建特定目标。

命令在这里

make -f CMakeFiles/Makefile2 -j2 a b

谢谢@Tsyvarev,您的评论很有帮助。