summaryrefslogtreecommitdiff
path: root/Modules/ProcessorCount.cmake
diff options
context:
space:
mode:
authorSylvain Joubert <joubert.sy@gmail.com>2019-02-27 12:47:07 +0100
committerSylvain Joubert <joubert.sy@gmail.com>2019-02-27 13:04:07 +0100
commitf20eab9cdc26ffbacb32e5942e2969058e50581a (patch)
tree47204ce04ccd53cc5bfd17a33853ec81649ef41b /Modules/ProcessorCount.cmake
parentb3191a0f5740d5efa5d6286d3f7c2b9356fa87c0 (diff)
downloadcmake-f20eab9cdc26ffbacb32e5942e2969058e50581a.tar.gz
ProcessorCount: Return the container CPU count instead of the host count
On Linux containers (tested with LXC and Docker) getconf returns the host CPU count. Use nproc with a higher priority if available to get the container's allocated CPUs instead of the non-accessible host count.
Diffstat (limited to 'Modules/ProcessorCount.cmake')
-rw-r--r--Modules/ProcessorCount.cmake14
1 files changed, 14 insertions, 0 deletions
diff --git a/Modules/ProcessorCount.cmake b/Modules/ProcessorCount.cmake
index e4b4e53725..8c252566a5 100644
--- a/Modules/ProcessorCount.cmake
+++ b/Modules/ProcessorCount.cmake
@@ -70,6 +70,20 @@ function(ProcessorCount var)
endif()
if(NOT count)
+ # Linux (systems with nproc):
+ # Prefer nproc to getconf if available as getconf may return the host CPU count in Linux containers
+ find_program(ProcessorCount_cmd_nproc nproc)
+ mark_as_advanced(ProcessorCount_cmd_nproc)
+ if(ProcessorCount_cmd_nproc)
+ execute_process(COMMAND ${ProcessorCount_cmd_nproc}
+ ERROR_QUIET
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ OUTPUT_VARIABLE count)
+ #message("ProcessorCount: trying nproc '${ProcessorCount_cmd_nproc}'")
+ endif()
+ endif()
+
+ if(NOT count)
# Linux (systems with getconf):
find_program(ProcessorCount_cmd_getconf getconf)
mark_as_advanced(ProcessorCount_cmd_getconf)