如何防止Cmake并行构建

问题描述 投票:0回答:1

我有一个顶级 CMakeLists.txt 文件,其中包含 2 个子目录。只要 -j 或 --parallel 不在 cmake 命令行上,它就可以正确构建。 CMakeLists.txt 文件记录了该问题。除了记录问题之外,还有其他方法可以防止在 CMakeLists.txt 文件中进行并行构建吗?

cmake_minimum_required(VERSION 3.25)

# Do not attempt to build in parallel using either --parallel or -j
# There can be a race codition between generating the performance test
# header file and building the tests.

project(TestDictionary LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(src/PerformanceTestGenerator)
add_subdirectory(src/tests)
cmake_minimum_required(VERSION 3.25)

project(createPerformanceTest LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(createPerformanceTest main.cpp PerformanceTestGenerator.cpp TestParameters.cpp)

target_compile_features(createPerformanceTest PUBLIC cxx_std_23)

if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
    # warning level 4 and all warnings as errors
    target_compile_options(createPerformanceTest PRIVATE /W3 /WX -D_CRT_SECURE_NO_WARNINGS)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        # require at least gcc 12 to get full C++20 support
        if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12)
            message(FATAL_ERROR "GCC version must be at least 12!")
        endif()
    endif()
    # lots of warnings and all warnings as errors
    target_compile_options(createPerformanceTest PRIVATE -Wall -Wextra -pedantic -Werror)
endif()

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/performanceTest.h
  COMMAND createPerformanceTest --output-file ${CMAKE_CURRENT_SOURCE_DIR}/performanceTest.h --test-count 3 --radix-16 --test-size 10 --test-size 20 --test-size ff --both-tests
  COMMENT "Generating performanceTest.h header file"
  VERBATIM
)

add_custom_target(
  GeneratedHeader ALL
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/performanceTest.h
)
cmake_minimum_required(VERSION 3.25)

project(testTest LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(testTest testtest.cpp FunctionalityTests.cpp TestGenericDictionary.cpp)

target_include_directories(testTest
    PUBLIC ../..
    PRIVATE ./ ../PerformanceTestGenerator 
)

target_compile_features(testTest PUBLIC cxx_std_23)

if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
    # warning level 4 and all warnings as errors
    target_compile_options(testTest PRIVATE /W4 /WX -D_CRT_SECURE_NO_WARNINGS)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        # require at least gcc 12 to get full C++20 support
        if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12)
            message(FATAL_ERROR "GCC version must be at least 12!")
        endif()
    endif()
    # lots of warnings and all warnings as errors
    target_compile_options(testTest PRIVATE -Wall -Wextra -pedantic -Werror)
endif()
cmake parallel-processing
1个回答
0
投票

不,您无法阻止并行构建。 CMake 将创建构建文件,例如 Makefile 或 Ninja 文件。这些工具决定如何构建目标。您可以告诉 make 和 ninja 使用单个线程。没有通用的方法。

如果你是

cmake --build
而不是调用原生构建工具,你可以将
CMAKE_BUILD_PARALLEL_LEVEL
设置为1。不过,这个必须设置,并且不能在配置时设置。

© www.soinside.com 2019 - 2024. All rights reserved.