diff --git a/CMakeLists.txt b/CMakeLists.txt index d6018f994ac..bff524d66cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,11 +158,24 @@ if(CMAKE_COMPILER_IS_MSVC) include(ProcessorCount) ProcessorCount(CPUCores) set(MSVC_MP ${CPUCores} CACHE STRING "Number of simultaneously running compilers (0 = automatic detection by MSVC). See documentation of /MP flag.") - if(MSVC_MP EQUAL 0) - # MSVC_MP is 0 in case the information cannot be determined by ProcessorCount => fallback - add_compile_options("/MP") - elseif(MSVC_MP GREATER 1) - add_compile_options("/MP${MSVC_MP}") + if (CMAKE_VERSION VERSION_LESS 3.11.0) + # Usage of COMPILE_LANGUAGE generator expression for MSVC in add_compile_options requires at least CMake 3.11, see https://gitlab.kitware.com/cmake/cmake/issues/17435 + if(MSVC_MP EQUAL 0) + # MSVC_MP is 0 in case the information cannot be determined by ProcessorCount => fallback + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") + elseif(MSVC_MP GREATER 1) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP${MSVC_MP}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP${MSVC_MP}") + endif() + else() + if(MSVC_MP EQUAL 0) + # MSVC_MP is 0 in case the information cannot be determined by ProcessorCount => fallback + # Generator expression is necessary to limit /MP flag to C/CXX, so flag will be not set to e.g. CUDA (see https://gitlab.kitware.com/cmake/cmake/issues/17535) + add_compile_options($<$,$>:/MP>) + elseif(MSVC_MP GREATER 1) + add_compile_options($<$,$>:/MP${MSVC_MP}>) + endif() endif() endif()