2018-12-23 14:19:11 +00:00
|
|
|
# Usage:
|
|
|
|
# set (MAX_COMPILER_MEMORY 2000 CACHE INTERNAL "") # In megabytes
|
|
|
|
# set (MAX_LINKER_MEMORY 3500 CACHE INTERNAL "")
|
|
|
|
# include (cmake/limit_jobs.cmake)
|
|
|
|
|
|
|
|
cmake_host_system_information(RESULT AVAILABLE_PHYSICAL_MEMORY QUERY AVAILABLE_PHYSICAL_MEMORY) # Not available under freebsd
|
2019-01-11 12:40:19 +00:00
|
|
|
cmake_host_system_information(RESULT NUMBER_OF_LOGICAL_CORES QUERY NUMBER_OF_LOGICAL_CORES)
|
2018-12-23 14:19:11 +00:00
|
|
|
|
2020-09-17 15:37:23 +00:00
|
|
|
# 1 if not set
|
|
|
|
option(PARALLEL_COMPILE_JOBS "Maximum number of concurrent compilation jobs" "")
|
|
|
|
|
|
|
|
# 1 if not set
|
|
|
|
option(PARALLEL_LINK_JOBS "Maximum number of concurrent link jobs" "")
|
|
|
|
|
2018-12-27 15:23:37 +00:00
|
|
|
if (NOT PARALLEL_COMPILE_JOBS AND AVAILABLE_PHYSICAL_MEMORY AND MAX_COMPILER_MEMORY)
|
|
|
|
math(EXPR PARALLEL_COMPILE_JOBS ${AVAILABLE_PHYSICAL_MEMORY}/${MAX_COMPILER_MEMORY})
|
2020-09-17 15:37:23 +00:00
|
|
|
|
2018-12-23 14:19:11 +00:00
|
|
|
if (NOT PARALLEL_COMPILE_JOBS)
|
|
|
|
set (PARALLEL_COMPILE_JOBS 1)
|
|
|
|
endif ()
|
|
|
|
endif ()
|
2020-09-17 15:37:23 +00:00
|
|
|
|
2019-01-11 12:40:19 +00:00
|
|
|
if (PARALLEL_COMPILE_JOBS AND (NOT NUMBER_OF_LOGICAL_CORES OR PARALLEL_COMPILE_JOBS LESS NUMBER_OF_LOGICAL_CORES))
|
2018-12-27 15:23:37 +00:00
|
|
|
set(CMAKE_JOB_POOL_COMPILE compile_job_pool${CMAKE_CURRENT_SOURCE_DIR})
|
2018-12-27 18:21:06 +00:00
|
|
|
string (REGEX REPLACE "[^a-zA-Z0-9]+" "_" CMAKE_JOB_POOL_COMPILE ${CMAKE_JOB_POOL_COMPILE})
|
2018-12-27 15:23:37 +00:00
|
|
|
set_property(GLOBAL APPEND PROPERTY JOB_POOLS ${CMAKE_JOB_POOL_COMPILE}=${PARALLEL_COMPILE_JOBS})
|
2018-12-23 14:19:11 +00:00
|
|
|
endif ()
|
|
|
|
|
2020-09-17 15:37:23 +00:00
|
|
|
|
2018-12-27 15:23:37 +00:00
|
|
|
if (NOT PARALLEL_LINK_JOBS AND AVAILABLE_PHYSICAL_MEMORY AND MAX_LINKER_MEMORY)
|
|
|
|
math(EXPR PARALLEL_LINK_JOBS ${AVAILABLE_PHYSICAL_MEMORY}/${MAX_LINKER_MEMORY})
|
2020-09-17 15:37:23 +00:00
|
|
|
|
2018-12-23 14:19:11 +00:00
|
|
|
if (NOT PARALLEL_LINK_JOBS)
|
|
|
|
set (PARALLEL_LINK_JOBS 1)
|
|
|
|
endif ()
|
|
|
|
endif ()
|
2020-09-17 15:37:23 +00:00
|
|
|
|
2020-12-10 18:37:12 +00:00
|
|
|
# ThinLTO provides its own parallel linking
|
2020-12-24 19:51:34 +00:00
|
|
|
# (which is enabled only for RELWITHDEBINFO)
|
|
|
|
#
|
2020-12-10 18:37:12 +00:00
|
|
|
# But use 2 parallel jobs, since:
|
|
|
|
# - this is what llvm does
|
|
|
|
# - and I've verfied that lld-11 does not use all available CPU time (in peak) while linking one binary
|
2020-12-24 19:51:34 +00:00
|
|
|
if (CMAKE_BUILD_TYPE_UC STREQUAL "RELWITHDEBINFO" AND ENABLE_THINLTO AND PARALLEL_LINK_JOBS GREATER 2)
|
2020-12-10 18:37:12 +00:00
|
|
|
message(STATUS "ThinLTO provides its own parallel linking - limiting parallel link jobs to 2.")
|
|
|
|
set (PARALLEL_LINK_JOBS 2)
|
|
|
|
endif()
|
|
|
|
|
2020-12-24 19:52:39 +00:00
|
|
|
if (PARALLEL_LINK_JOBS AND (NOT NUMBER_OF_LOGICAL_CORES OR PARALLEL_LINK_JOBS LESS NUMBER_OF_LOGICAL_CORES))
|
2018-12-27 15:23:37 +00:00
|
|
|
set(CMAKE_JOB_POOL_LINK link_job_pool${CMAKE_CURRENT_SOURCE_DIR})
|
2018-12-27 18:21:06 +00:00
|
|
|
string (REGEX REPLACE "[^a-zA-Z0-9]+" "_" CMAKE_JOB_POOL_LINK ${CMAKE_JOB_POOL_LINK})
|
2018-12-27 15:23:37 +00:00
|
|
|
set_property(GLOBAL APPEND PROPERTY JOB_POOLS ${CMAKE_JOB_POOL_LINK}=${PARALLEL_LINK_JOBS})
|
2018-12-23 14:19:11 +00:00
|
|
|
endif ()
|
|
|
|
|
2019-01-11 12:40:19 +00:00
|
|
|
if (PARALLEL_COMPILE_JOBS OR PARALLEL_LINK_JOBS)
|
2020-09-17 15:37:23 +00:00
|
|
|
message(STATUS
|
|
|
|
"${CMAKE_CURRENT_SOURCE_DIR}: Have ${AVAILABLE_PHYSICAL_MEMORY} megabytes of memory.
|
|
|
|
Limiting concurrent linkers jobs to ${PARALLEL_LINK_JOBS} and compiler jobs to ${PARALLEL_COMPILE_JOBS}")
|
2019-01-11 12:40:19 +00:00
|
|
|
endif ()
|