summaryrefslogtreecommitdiffstats
path: root/CompileFlags.cmake
diff options
context:
space:
mode:
authorRobert Dailey <rcdailey@gmail.com>2018-01-29 17:34:28 (GMT)
committerRobert Dailey <rcdailey@gmail.com>2018-02-15 16:45:37 (GMT)
commita1dee224b8f92a16a84ac285620136d04adb880a (patch)
tree2207f860ef9062a3cf79683537f5225ea008ecee /CompileFlags.cmake
parenta2ec98b7d97df67d50e90e790b2bd235f7c278cc (diff)
downloadCMake-a1dee224b8f92a16a84ac285620136d04adb880a.zip
CMake-a1dee224b8f92a16a84ac285620136d04adb880a.tar.gz
CMake-a1dee224b8f92a16a84ac285620136d04adb880a.tar.bz2
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
Diffstat (limited to 'CompileFlags.cmake')
-rw-r--r--CompileFlags.cmake23
1 files changed, 23 insertions, 0 deletions
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()