ProcessorCount.cmake 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. message("ProcessorCount: using environment variable")
  30. elseif(APPLE)
  31. # Mac:
  32. find_program(ProcessorCount_cmd_sysctl sysctl
  33. PATHS /usr/sbin)
  34. if(ProcessorCount_cmd_sysctl)
  35. execute_process(COMMAND ${ProcessorCount_cmd_sysctl} -n hw.ncpu
  36. OUTPUT_STRIP_TRAILING_WHITESPACE
  37. OUTPUT_VARIABLE count)
  38. message("ProcessorCount: using sysctl '${ProcessorCount_cmd_sysctl}'")
  39. endif()
  40. else()
  41. # Linux (systems with getconf):
  42. find_program(ProcessorCount_cmd_getconf getconf)
  43. if(ProcessorCount_cmd_getconf)
  44. execute_process(COMMAND ${ProcessorCount_cmd_getconf} _NPROCESSORS_ONLN
  45. OUTPUT_STRIP_TRAILING_WHITESPACE
  46. OUTPUT_VARIABLE count)
  47. message("ProcessorCount: using getconf '${ProcessorCount_cmd_getconf}'")
  48. endif()
  49. if(NOT count)
  50. # QNX (systems with pidin):
  51. find_program(ProcessorCount_cmd_pidin pidin)
  52. if(ProcessorCount_cmd_pidin)
  53. execute_process(COMMAND ${ProcessorCount_cmd_pidin} info
  54. OUTPUT_STRIP_TRAILING_WHITESPACE
  55. OUTPUT_VARIABLE pidin_output)
  56. string(REGEX MATCHALL "Processor[0-9]+: " procs "${pidin_output}")
  57. list(LENGTH procs count)
  58. message("ProcessorCount: using pidin '${ProcessorCount_cmd_pidin}'")
  59. endif()
  60. endif()
  61. endif()
  62. # Execute this code when there is no 'sysctl' or 'getconf' or 'pidin' or
  63. # when previously executed methods return empty output:
  64. #
  65. if(NOT count)
  66. # Systems with /proc/cpuinfo:
  67. set(cpuinfo_file /proc/cpuinfo)
  68. if(EXISTS "${cpuinfo_file}")
  69. file(STRINGS "${cpuinfo_file}" procs REGEX "^processor.: [0-9]+$")
  70. list(LENGTH procs count)
  71. message("ProcessorCount: using cpuinfo '${cpuinfo_file}'")
  72. endif()
  73. endif()
  74. # Ensure an integer return (avoid inadvertently returning an empty string
  75. # or an error string)... If it's not a decimal integer, return 0:
  76. #
  77. if(NOT count MATCHES "^[0-9]+$")
  78. set(count 0)
  79. endif()
  80. set(${var} ${count} PARENT_SCOPE)
  81. endfunction()