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

问题描述

这是How to build specific multiple targets parallelly using make?

的相关问题

多个目标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,您的评论非常有帮助。