title: How do I configure portable parallel builds in CMake?
body: <p>Is it somehow possible to be able to have a parallel build no matter which build tool is used?</p>
  
  <p>Under Unix we can add <code>make -jN</code> where N are the number of threads, and under Windows I added to the <code>CXX_FLAG "/MP"</code> which is then used in Visual Studio to parallel build...(?) How can I make my version such that <code>CMAKE_MAKE_PROGRAM</code> is not always extended when I run CMake?</p>
  
  <p>What is a general solution?</p>
  
  <p>I came up with this:</p>
  
  <pre><code># Add some multithreaded build support
  MARK_AS_ADVANCED(MULTITHREADED_BUILD)
  set(MULTITHREADED_BUILD 12 CACHE STRING "How many threads are used to build the project")
  if(MULTITHREADED_BUILD)
      if(${CMAKE_GENERATOR} MATCHES "Unix Makefiles")
              message(STATUS ${CMAKE_BUILD_TOOL})
              set(CMAKE_MAKE_PROGRAM "${CMAKE_MAKE_PROGRAM} -j${MULTITHREADED_BUILD}")
              message(STATUS "Added arguments to CMAKE_BUILD_TOOL: ${CMAKE_MAKE_PROGRAM}")
      elseif(MSVC)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
        message(STATUS "Added parallel build arguments to CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
      endif()
  endif()
  </code></pre>