ProcessorCount.cmake 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # - ProcessorCount(var)
  2. # Determine the number of processors/cores and save value in ${var}
  3. #
  4. # Sets the variable named ${var} to the number of physical cores available on
  5. # the machine if the information can be determined. Otherwise it is set to 0.
  6. # Currently this functionality is only implemented for Windows, Mac OS X and
  7. # Unix systems providing getconf or the /proc/cpuinfo interface (e.g. Linux).
  8. # A more reliable way might be to compile a small C program that uses the CPUID
  9. # instruction, but that again requires compiler support or compiling assembler
  10. # code.
  11. #=============================================================================
  12. # Copyright 2010 Kitware, Inc.
  13. #
  14. # Distributed under the OSI-approved BSD License (the "License");
  15. # see accompanying file Copyright.txt for details.
  16. #
  17. # This software is distributed WITHOUT ANY WARRANTY; without even the
  18. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. # See the License for more information.
  20. #=============================================================================
  21. # (To distribute this file outside of CMake, substitute the full
  22. # License text for the above reference.)
  23. function(ProcessorCount var)
  24. # Unknown:
  25. set(count 0)
  26. if(WIN32)
  27. # Windows:
  28. set(count "$ENV{NUMBER_OF_PROCESSORS}")
  29. elseif(APPLE)
  30. # Mac:
  31. find_program(ProcessorCount_cmd_sysctl sysctl
  32. PATHS /usr/sbin)
  33. if(ProcessorCount_cmd_sysctl)
  34. execute_process(COMMAND ${ProcessorCount_cmd_sysctl} -n hw.ncpu
  35. OUTPUT_STRIP_TRAILING_WHITESPACE
  36. OUTPUT_VARIABLE count)
  37. endif()
  38. else()
  39. find_program(ProcessorCount_cmd_getconf getconf)
  40. if(ProcessorCount_cmd_getconf)
  41. # Linux and other systems with getconf:
  42. execute_process(COMMAND ${ProcessorCount_cmd_getconf} _NPROCESSORS_ONLN
  43. OUTPUT_STRIP_TRAILING_WHITESPACE
  44. OUTPUT_VARIABLE count)
  45. else()
  46. # Linux and other systems with /proc/cpuinfo:
  47. set(cpuinfo_file /proc/cpuinfo)
  48. if(EXISTS "${cpuinfo_file}")
  49. file(STRINGS "${cpuinfo_file}" procs REGEX "^processor.: [0-9]+$")
  50. list(LENGTH procs count)
  51. endif()
  52. endif()
  53. endif()
  54. set(${var} ${count} PARENT_SCOPE)
  55. endfunction()