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)
2022-08-13 04:39:20 +00:00
cmake_host_system_information ( RESULT TOTAL_PHYSICAL_MEMORY QUERY TOTAL_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" "" )
2022-08-13 04:39:20 +00:00
if ( NOT PARALLEL_COMPILE_JOBS AND TOTAL_PHYSICAL_MEMORY AND MAX_COMPILER_MEMORY )
math ( EXPR PARALLEL_COMPILE_JOBS ${ TOTAL_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 ( )
2023-07-02 14:51:43 +00:00
if ( NOT NUMBER_OF_LOGICAL_CORES OR PARALLEL_COMPILE_JOBS LESS NUMBER_OF_LOGICAL_CORES )
set ( PARALLEL_COMPILE_JOBS_LESS TRUE )
endif ( )
2018-12-23 14:19:11 +00:00
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
2022-08-13 04:39:20 +00:00
if ( NOT PARALLEL_LINK_JOBS AND TOTAL_PHYSICAL_MEMORY AND MAX_LINKER_MEMORY )
math ( EXPR PARALLEL_LINK_JOBS ${ TOTAL_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 ( )
2023-07-02 14:51:43 +00:00
if ( NOT NUMBER_OF_LOGICAL_CORES OR PARALLEL_LINK_JOBS LESS NUMBER_OF_LOGICAL_CORES )
set ( PARALLEL_LINK_JOBS_LESS TRUE )
endif ( )
2018-12-23 14:19:11 +00:00
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
2022-08-13 04:39:20 +00:00
" $ { C M A K E _ C U R R E N T _ S O U R C E _ D I R } : H a v e $ { T O T A L _ P H Y S I C A L _ M E M O R Y } m e g a b y t e s o f m e m o r y .
2022-03-01 20:21:54 +00:00
L i m i t i n g c o n c u r r e n t l i n k e r s j o b s t o $ { P A R A L L E L _ L I N K _ J O B S } a n d c o m p i l e r j o b s t o $ { P A R A L L E L _ C O M P I L E _ J O B S } ( s y s t e m h a s $ { N U M B E R _ O F _ L O G I C A L _ C O R E S } l o g i c a l c o r e s ) " )
2023-07-02 14:51:43 +00:00
if ( PARALLEL_COMPILE_JOBS_LESS )
2023-07-02 14:59:48 +00:00
message ( WARNING "The autocalculated compile jobs limit (${PARALLEL_COMPILE_JOBS}) underutilizes CPU cores (${NUMBER_OF_LOGICAL_CORES}). Set PARALLEL_COMPILE_JOBS to override." )
2023-07-02 14:51:43 +00:00
endif ( )
if ( PARALLEL_LINK_JOBS_LESS )
2023-07-02 14:59:48 +00:00
message ( WARNING "The autocalculated link jobs limit (${PARALLEL_LINK_JOBS}) underutilizes CPU cores (${NUMBER_OF_LOGICAL_CORES}). Set PARALLEL_LINK_JOBS to override." )
2023-07-02 14:51:43 +00:00
endif ( )
2019-01-11 12:40:19 +00:00
endif ( )