cmake: Intelligently set job limits for sccache

If we are correctly configured for distributed mode, use the cluster's CPU count
instead of ours. If we are configured for sccache but without distributed mode,
inform the user but continue with normal job limits.

Signed-off-by: Zack Cerza <zack@redhat.com>
This commit is contained in:
Zack Cerza 2024-04-04 13:01:05 -06:00
parent c2d8c3155d
commit be709462ad
2 changed files with 55 additions and 28 deletions

View File

@ -85,17 +85,36 @@ endif(WITH_CCACHE)
option(WITH_SCCACHE "Build with sccache.")
if(WITH_SCCACHE)
if(CMAKE_C_COMPILER_LAUNCHER OR CMAKE_CXX_COMPILER_LAUNCHER)
message(WARNING "Compiler launcher already set. stop configuring sccache")
else()
find_program(SCCACHE_EXECUTABLE sccache)
if(NOT SCCACHE_EXECUTABLE)
message(FATAL_ERROR "Can't find sccache. Is it installed?")
endif()
message(STATUS "Building with sccache: ${SCCACHE_EXECUTABLE}, SCCACHE_CONF=$ENV{SCCACHE_CONF}")
set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE})
set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE})
find_program(SCCACHE_EXECUTABLE sccache)
if(NOT SCCACHE_EXECUTABLE)
message(FATAL_ERROR "Can't find sccache. Is it installed?")
endif()
if(NOT NINJA_MAX_COMPILE_JOBS)
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19")
execute_process(
COMMAND "sccache" "--dist-status"
OUTPUT_VARIABLE sccache_dist_status
)
string(
JSON sccache_cores
ERROR_VARIABLE sccache_dist_status_error
GET "${sccache_dist_status}" SchedulerStatus 1 num_cpus
)
string(FIND "${sccache_dist_status}" "disabled" find_result)
if(find_result EQUAL -1)
message(STATUS "Using sccache with distributed compilation. Effective cores: ${sccache_cores}")
set(NINJA_MAX_COMPILE_JOBS ${sccache_cores})
set(NINJA_MAX_LINK_JOBS ${sccache_cores})
else()
message(WARNING "Using sccache, but it is not configured for distributed complilation")
endif()
else()
message(WARNING "Using sccache, but cannot determine maximum job value since cmake version is <3.19")
endif()
endif()
message(STATUS "Building with sccache: ${SCCACHE_EXECUTABLE}, SCCACHE_CONF=$ENV{SCCACHE_CONF}")
set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE})
set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE_EXECUTABLE})
endif(WITH_SCCACHE)
option(WITH_MANPAGE "Build man pages." ON)

View File

@ -4,16 +4,20 @@ set(MAX_LINK_MEM 4500 CACHE INTERNAL "maximum memory used by each linking job (i
cmake_host_system_information(RESULT _num_cores QUERY NUMBER_OF_LOGICAL_CORES)
cmake_host_system_information(RESULT _total_mem QUERY TOTAL_PHYSICAL_MEMORY)
math(EXPR _avg_compile_jobs "${_total_mem} / ${MAX_COMPILE_MEM}")
if(_avg_compile_jobs EQUAL 0)
set(_avg_compile_jobs 1)
if(NINJA_MAX_COMPILE_JOBS)
set(_avg_compile_jobs "${NINJA_MAX_COMPILE_JOBS}")
else()
math(EXPR _avg_compile_jobs "${_total_mem} / ${MAX_COMPILE_MEM}")
if(_avg_compile_jobs EQUAL 0)
set(_avg_compile_jobs 1)
endif()
if(_num_cores LESS _avg_compile_jobs)
set(_avg_compile_jobs "${_num_cores}")
endif()
set(NINJA_MAX_COMPILE_JOBS "${_avg_compile_jobs}" CACHE STRING
"The maximum number of concurrent compilation jobs, for Ninja build system." FORCE)
mark_as_advanced(NINJA_MAX_COMPILE_JOBS)
endif()
if(_num_cores LESS _avg_compile_jobs)
set(_avg_compile_jobs ${_num_cores})
endif()
set(NINJA_MAX_COMPILE_JOBS "${_avg_compile_jobs}" CACHE STRING
"The maximum number of concurrent compilation jobs, for Ninja build system." FORCE)
mark_as_advanced(NINJA_MAX_COMPILE_JOBS)
if(NINJA_MAX_COMPILE_JOBS)
math(EXPR _heavy_compile_jobs "${_avg_compile_jobs} / 2")
if(_heavy_compile_jobs EQUAL 0)
@ -25,16 +29,20 @@ if(NINJA_MAX_COMPILE_JOBS)
set(CMAKE_JOB_POOL_COMPILE avg_compile_job_pool)
endif()
math(EXPR _avg_link_jobs "${_total_mem} / ${MAX_LINK_MEM}")
if(_avg_link_jobs EQUAL 0)
set(_avg_link_jobs 1)
if(NINJA_MAX_LINK_JOBS)
set(_avg_link_jobs "${NINJA_MAX_LINK_JOBS}")
else()
math(EXPR _avg_link_jobs "${_total_mem} / ${MAX_LINK_MEM}")
if(_avg_link_jobs EQUAL 0)
set(_avg_link_jobs 1)
endif()
if(_num_cores LESS _avg_link_jobs)
set(_avg_link_jobs "${_num_cores}")
endif()
set(NINJA_MAX_LINK_JOBS "${_avg_link_jobs}" CACHE STRING
"The maximum number of concurrent link jobs, for Ninja build system." FORCE)
mark_as_advanced(NINJA_MAX_LINK_JOBS)
endif()
if(_num_cores LESS _avg_link_jobs)
set(_avg_link_jobs ${_num_cores})
endif()
set(NINJA_MAX_LINK_JOBS "${_avg_link_jobs}" CACHE STRING
"The maximum number of concurrent link jobs, for Ninja build system." FORCE)
mark_as_advanced(NINJA_MAX_LINK_JOBS)
if(NINJA_MAX_LINK_JOBS)
math(EXPR _heavy_link_jobs "${_avg_link_jobs} / 2")
if(_heavy_link_jobs EQUAL 0)