I have a top level CMakeLists.txt file that includes 2 sub directories. It builds properly as long as -j or --parallel are not on the cmake command line. The CMakeLists.txt file documents the issue. Other than documenting the issue is there a way to prevent parallel builds in the CMakeLists.txt file?
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()
No, you cannot prevent building in parallel. CMake will create build files like Makefile or Ninja files. These tools decide on how to build the targets. You can tell make and ninja to use a single thread. There is no generic way.
If you are cmake --build
instead of calling the native build tool, you can set CMAKE_BUILD_PARALLEL_LEVEL
to 1. Still, this has to be set and cannot be set during configure time.