如何配置便携式并行编译CMake的中?(How do I configure portable pa

2019-06-24 15:51发布

Is it somehow possible to be able to have a parallel build no matter which build tool is used?

Under Unix we can add make -jN where N are the number of threads, and under Windows I added to the CXX_FLAG "/MP" which is then used in Visual Studio to parallel build...(?) How can I make my version such that CMAKE_MAKE_PROGRAM is not always extended when I run CMake?

What is a general solution?

I came up with this:

# Add some multithreaded build support
MARK_AS_ADVANCED(MULTITHREADED_BUILD)
set(MULTITHREADED_BUILD 12 CACHE STRING "How many threads are used to build the project")
if(MULTITHREADED_BUILD)
    if(${CMAKE_GENERATOR} MATCHES "Unix Makefiles")
            message(STATUS ${CMAKE_BUILD_TOOL})
            set(CMAKE_MAKE_PROGRAM "${CMAKE_MAKE_PROGRAM} -j${MULTITHREADED_BUILD}")
            message(STATUS "Added arguments to CMAKE_BUILD_TOOL: ${CMAKE_MAKE_PROGRAM}")
    elseif(MSVC)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
      message(STATUS "Added parallel build arguments to CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
    endif()
endif()

Answer 1:

随着CMake的3.12,这是可能的。 从发行说明:

所述“cmake的(1)”的构建工具模式获得(“cmake的-build”)“ - 平行[]”和“-j []”选项以指定并行构建级别。 它们映射到相应的原始构建工具的选项。

编辑:正如@dkg提到你也可以设置环境变量CMAKE_BUILD_PARALLEL_LEVEL

链接到CMake的文档:

  • https://cmake.org/cmake/help/latest/manual/cmake.1.html#build-tool-mode
  • https://cmake.org/cmake/help/latest/envvar/CMAKE_BUILD_PARALLEL_LEVEL.html


Answer 2:

如果你有cmake v2.8.8或更高版本,你可以使用ninja作为替代GNU make

mkdir build
cd    build
cmake -G Ninja ..
ninja              # Parallel build (no need -j12)

要么

mkdir build
cd    build
cmake -G Ninja ..
cmake --build .    # Parallel build using ninja

正如你所看到的,没有必要使用CMAKE_MAKE_PROGRAM ,构建并行默认情况下运行,优化取决于可用的CPU内核的作业数。

ninja是基于低级JSON配置以加速启动阶段。 因此它的JSON配置是不容易手工编写和我使用的高级工具/ IDE总是产生的:

  • CMake的v2.8.8 (2012)
  • Qt Creator的V2.6 (2012年)
  • KDevelop的V4.6 (2013年)
  • ...看到的发电机ninja配置在https://github.com/ninja-build/ninja/wiki/List-of-generators-producing-ninja-build-files

由于C ++建立往往需要大量的内存,您的计算机必须提供尽可能多的内存CPU内核的数量。



Answer 3:

你可以这样做跨平台的。 该-jN选项是一个参数,使,而不是生成的Makefile的一部分。 但是,你可以有CMake的生成运行make使用-jN(其中的脚本查找核心的你有号码)项目中的bash脚本。



Answer 4:

我已经定下心来写parallelmake.sh脚本Unix Makefiles为基础的发电机。 这是在这里完成: https://github.com/gabyx/ApproxMVBB

而在CMake的文件中的相关部分:

https://github.com/gabyx/ApproxMVBB/blob/master/CMakeLists.txt#L89

# Add some multithreaded build support =====================================================================================================
MARK_AS_ADVANCED(MULTITHREADED_BUILD)
SET(MULTITHREADED_BUILD ON CACHE BOOL "Parallel build with as many threads as possible!")
if(MULTITHREADED_BUILD)
    if(${CMAKE_GENERATOR} MATCHES "Unix Makefiles")
            file(COPY ${ApproxMVBB_ROOT_DIR}/cmake/parallelmake.sh DESTINATION ${PROJECT_BINARY_DIR}
                FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
                NO_SOURCE_PERMISSIONS
            )
            SET(CMAKE_MAKE_PROGRAM "${PROJECT_BINARY_DIR}/parallelmake.sh")
            MESSAGE(STATUS "Set make program to ${PROJECT_BINARY_DIR}/parallelmake.sh")
    elseif(MSVC)
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" "/MP")
      MESSAGE(STATUS "Added parallel build arguments to CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
    endif()
endif()
# ========================================================================================================================================


文章来源: How do I configure portable parallel builds in CMake?