From a1dee224b8f92a16a84ac285620136d04adb880a Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Mon, 29 Jan 2018 11:34:28 -0600 Subject: CMake: Enable /MP for MSVC toolchain A new cache option named `CMake_MSVC_PARALLEL` is now available that can be used to control the usage of [`/MP`][1] to the MSVC compiler. This enables parallelized builds on a per-translation unit basis. To enable `/MP`, specify value `ON` to the option. Using an integral non-zero value will control the specific number of threads used with the `/MP` option, as opposed to letting the toolchain decide for you. Fixes #17696 [1]: https://docs.microsoft.com/en-us/cpp/build/reference/mp-build-with-multiple-processes --- CompileFlags.cmake | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CompileFlags.cmake b/CompileFlags.cmake index 32e7005..ec9b31b 100644 --- a/CompileFlags.cmake +++ b/CompileFlags.cmake @@ -82,3 +82,26 @@ endif () if (CMAKE_ANSI_CFLAGS) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_ANSI_CFLAGS}") endif () + +# Allow per-translation-unit parallel builds when using MSVC +if(CMAKE_GENERATOR MATCHES "Visual Studio" AND + (CMAKE_C_COMPILER_ID MATCHES "MSVC|Intel" OR + CMAKE_CXX_COMPILER_ID MATCHES "MSVC|Intel")) + + set(CMake_MSVC_PARALLEL ON CACHE STRING "\ +Enables /MP flag for parallel builds using MSVC. Specify an \ +integer value to control the number of threads used (Only \ +works on some older versions of Visual Studio). Setting to \ +ON lets the toolchain decide how many threads to use. Set to \ +OFF to disable /MP completely." ) + + if(CMake_MSVC_PARALLEL) + if(CMake_MSVC_PARALLEL GREATER 0) + string(APPEND CMAKE_C_FLAGS " /MP${CMake_MSVC_PARALLEL}") + string(APPEND CMAKE_CXX_FLAGS " /MP${CMake_MSVC_PARALLEL}") + else() + string(APPEND CMAKE_C_FLAGS " /MP") + string(APPEND CMAKE_CXX_FLAGS " /MP") + endif() + endif() +endif() -- cgit v0.12