cmake_host_system_information.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. cmake_host_system_information
  2. -----------------------------
  3. Query various host system information.
  4. Synopsis
  5. ^^^^^^^^
  6. .. parsed-literal::
  7. `Query host system specific information`_
  8. cmake_host_system_information(RESULT <variable> QUERY <key> ...)
  9. `Query Windows registry`_
  10. cmake_host_system_information(RESULT <variable> QUERY WINDOWS_REGISTRY <key> ...)
  11. Query host system specific information
  12. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  13. .. code-block:: cmake
  14. cmake_host_system_information(RESULT <variable> QUERY <key> ...)
  15. Queries system information of the host system on which cmake runs.
  16. One or more ``<key>`` can be provided to select the information to be
  17. queried. The list of queried values is stored in ``<variable>``.
  18. ``<key>`` can be one of the following values:
  19. ``NUMBER_OF_LOGICAL_CORES``
  20. Number of logical cores
  21. ``NUMBER_OF_PHYSICAL_CORES``
  22. Number of physical cores
  23. ``HOSTNAME``
  24. Hostname
  25. ``FQDN``
  26. Fully qualified domain name
  27. ``TOTAL_VIRTUAL_MEMORY``
  28. Total virtual memory in MiB [#mebibytes]_
  29. ``AVAILABLE_VIRTUAL_MEMORY``
  30. Available virtual memory in MiB [#mebibytes]_
  31. ``TOTAL_PHYSICAL_MEMORY``
  32. Total physical memory in MiB [#mebibytes]_
  33. ``AVAILABLE_PHYSICAL_MEMORY``
  34. Available physical memory in MiB [#mebibytes]_
  35. ``IS_64BIT``
  36. .. versionadded:: 3.10
  37. One if processor is 64Bit
  38. ``HAS_FPU``
  39. .. versionadded:: 3.10
  40. One if processor has floating point unit
  41. ``HAS_MMX``
  42. .. versionadded:: 3.10
  43. One if processor supports MMX instructions
  44. ``HAS_MMX_PLUS``
  45. .. versionadded:: 3.10
  46. One if processor supports Ext. MMX instructions
  47. ``HAS_SSE``
  48. .. versionadded:: 3.10
  49. One if processor supports SSE instructions
  50. ``HAS_SSE2``
  51. .. versionadded:: 3.10
  52. One if processor supports SSE2 instructions
  53. ``HAS_SSE_FP``
  54. .. versionadded:: 3.10
  55. One if processor supports SSE FP instructions
  56. ``HAS_SSE_MMX``
  57. .. versionadded:: 3.10
  58. One if processor supports SSE MMX instructions
  59. ``HAS_AMD_3DNOW``
  60. .. versionadded:: 3.10
  61. One if processor supports 3DNow instructions
  62. ``HAS_AMD_3DNOW_PLUS``
  63. .. versionadded:: 3.10
  64. One if processor supports 3DNow+ instructions
  65. ``HAS_IA64``
  66. .. versionadded:: 3.10
  67. One if IA64 processor emulating x86
  68. ``HAS_SERIAL_NUMBER``
  69. .. versionadded:: 3.10
  70. One if processor has serial number
  71. ``PROCESSOR_SERIAL_NUMBER``
  72. .. versionadded:: 3.10
  73. Processor serial number
  74. ``PROCESSOR_NAME``
  75. .. versionadded:: 3.10
  76. Human readable processor name
  77. ``PROCESSOR_DESCRIPTION``
  78. .. versionadded:: 3.10
  79. Human readable full processor description
  80. ``OS_NAME``
  81. .. versionadded:: 3.10
  82. The host operating system name:
  83. * On UNIX platforms, this is ``uname -s``.
  84. * On Apple platforms, this is ``sw_vers -productName``.
  85. * On Windows, this is ``Windows``.
  86. See also :variable:`CMAKE_HOST_SYSTEM_NAME`.
  87. ``OS_RELEASE``
  88. .. versionadded:: 3.10
  89. The OS sub-type e.g. on Windows ``Professional``
  90. ``OS_VERSION``
  91. .. versionadded:: 3.10
  92. The OS build ID
  93. ``OS_PLATFORM``
  94. .. versionadded:: 3.10
  95. See :variable:`CMAKE_HOST_SYSTEM_PROCESSOR`
  96. ``MSYSTEM_PREFIX``
  97. .. versionadded:: 3.28
  98. Available only on Windows hosts. In a MSYS or MinGW development
  99. environment that sets the ``MSYSTEM`` environment variable, this
  100. is its installation prefix. Otherwise, this is the empty string.
  101. ``DISTRIB_INFO``
  102. .. versionadded:: 3.22
  103. Read :file:`/etc/os-release` file and define the given ``<variable>``
  104. into a list of read variables
  105. ``DISTRIB_<name>``
  106. .. versionadded:: 3.22
  107. Get the ``<name>`` variable (see `man 5 os-release`_) if it exists in the
  108. :file:`/etc/os-release` file
  109. Example:
  110. .. code-block:: cmake
  111. cmake_host_system_information(RESULT PRETTY_NAME QUERY DISTRIB_PRETTY_NAME)
  112. message(STATUS "${PRETTY_NAME}")
  113. cmake_host_system_information(RESULT DISTRO QUERY DISTRIB_INFO)
  114. foreach(VAR IN LISTS DISTRO)
  115. message(STATUS "${VAR}=`${${VAR}}`")
  116. endforeach()
  117. Output::
  118. -- Ubuntu 20.04.2 LTS
  119. -- DISTRO_BUG_REPORT_URL=`https://bugs.launchpad.net/ubuntu/`
  120. -- DISTRO_HOME_URL=`https://www.ubuntu.com/`
  121. -- DISTRO_ID=`ubuntu`
  122. -- DISTRO_ID_LIKE=`debian`
  123. -- DISTRO_NAME=`Ubuntu`
  124. -- DISTRO_PRETTY_NAME=`Ubuntu 20.04.2 LTS`
  125. -- DISTRO_PRIVACY_POLICY_URL=`https://www.ubuntu.com/legal/terms-and-policies/privacy-policy`
  126. -- DISTRO_SUPPORT_URL=`https://help.ubuntu.com/`
  127. -- DISTRO_UBUNTU_CODENAME=`focal`
  128. -- DISTRO_VERSION=`20.04.2 LTS (Focal Fossa)`
  129. -- DISTRO_VERSION_CODENAME=`focal`
  130. -- DISTRO_VERSION_ID=`20.04`
  131. If :file:`/etc/os-release` file is not found, the command tries to gather OS
  132. identification via fallback scripts. The fallback script can use `various
  133. distribution-specific files`_ to collect OS identification data and map it
  134. into `man 5 os-release`_ variables.
  135. Fallback Interface Variables
  136. """"""""""""""""""""""""""""
  137. .. variable:: CMAKE_GET_OS_RELEASE_FALLBACK_SCRIPTS
  138. In addition to the scripts shipped with CMake, a user may append full
  139. paths of their script(s) to this list. The script filename has the
  140. following format: ``NNN-<name>.cmake``, where ``NNN`` is three digits
  141. used to apply collected scripts in a specific order.
  142. .. variable:: CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_<varname>
  143. Variables collected by the user provided fallback script
  144. ought to be assigned to CMake variables using this naming
  145. convention. Example, the ``ID`` variable from the manual becomes
  146. ``CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_ID``.
  147. .. variable:: CMAKE_GET_OS_RELEASE_FALLBACK_RESULT
  148. The fallback script ought to store names of all assigned
  149. ``CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_<varname>`` variables in this list.
  150. Example:
  151. .. code-block:: cmake
  152. # 000-FallbackScript.cmake
  153. #
  154. # Try to detect some old distribution
  155. # See also
  156. # - http://linuxmafia.com/faq/Admin/release-files.html
  157. #
  158. if(NOT EXISTS "${CMAKE_SYSROOT}/etc/foobar-release")
  159. return()
  160. endif()
  161. # Get the first string only
  162. file(
  163. STRINGS "${CMAKE_SYSROOT}/etc/foobar-release" CMAKE_GET_OS_RELEASE_FALLBACK_CONTENT
  164. LIMIT_COUNT 1
  165. )
  166. #
  167. # Example:
  168. #
  169. # Foobar distribution release 1.2.3 (server)
  170. #
  171. if(CMAKE_GET_OS_RELEASE_FALLBACK_CONTENT MATCHES "Foobar distribution release ([0-9\.]+) .*")
  172. set(CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_NAME Foobar)
  173. set(CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_PRETTY_NAME "${CMAKE_GET_OS_RELEASE_FALLBACK_CONTENT}")
  174. set(CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_ID foobar)
  175. set(CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_VERSION ${CMAKE_MATCH_1})
  176. set(CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_VERSION_ID ${CMAKE_MATCH_1})
  177. list(
  178. APPEND CMAKE_GET_OS_RELEASE_FALLBACK_RESULT
  179. CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_NAME
  180. CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_PRETTY_NAME
  181. CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_ID
  182. CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_VERSION
  183. CMAKE_GET_OS_RELEASE_FALLBACK_RESULT_VERSION_ID
  184. )
  185. endif()
  186. unset(CMAKE_GET_OS_RELEASE_FALLBACK_CONTENT)
  187. Then this script can be applied as a fallback to determine the missing host
  188. system information:
  189. .. code-block:: cmake
  190. list(
  191. APPEND
  192. CMAKE_GET_OS_RELEASE_FALLBACK_SCRIPTS
  193. ${CMAKE_CURRENT_SOURCE_DIR}/000-FallbackScript.cmake
  194. )
  195. cmake_host_system_information(RESULT info QUERY DISTRIB_INFO)
  196. .. rubric:: Footnotes
  197. .. [#mebibytes] One MiB (mebibyte) is equal to 1024x1024 bytes.
  198. .. _man 5 os-release: https://www.freedesktop.org/software/systemd/man/latest/os-release.html
  199. .. _various distribution-specific files: http://linuxmafia.com/faq/Admin/release-files.html
  200. .. _Query Windows registry:
  201. Query Windows registry
  202. ^^^^^^^^^^^^^^^^^^^^^^
  203. .. versionadded:: 3.24
  204. .. code-block:: cmake
  205. cmake_host_system_information(RESULT <variable>
  206. QUERY WINDOWS_REGISTRY <key> [VALUE_NAMES|SUBKEYS|VALUE <name>]
  207. [VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
  208. [SEPARATOR <separator>]
  209. [ERROR_VARIABLE <result>])
  210. Performs query operations on local computer registry subkey. Returns a list of
  211. subkeys or value names that are located under the specified subkey in the
  212. registry or the data of the specified value name. The result of the queried
  213. entity is stored in ``<variable>``.
  214. .. note::
  215. Querying registry for any other platforms than ``Windows``, including
  216. ``CYGWIN``, will always returns an empty string and sets an error message in
  217. the variable specified with sub-option ``ERROR_VARIABLE``.
  218. ``<key>`` specify the full path of a subkey on the local computer. The
  219. ``<key>`` must include a valid root key. Valid root keys for the local computer
  220. are:
  221. * ``HKLM`` or ``HKEY_LOCAL_MACHINE``
  222. * ``HKCU`` or ``HKEY_CURRENT_USER``
  223. * ``HKCR`` or ``HKEY_CLASSES_ROOT``
  224. * ``HKU`` or ``HKEY_USERS``
  225. * ``HKCC`` or ``HKEY_CURRENT_CONFIG``
  226. And, optionally, the path to a subkey under the specified root key. The path
  227. separator can be the slash or the backslash. ``<key>`` is not case sensitive.
  228. For example:
  229. .. code-block:: cmake
  230. cmake_host_system_information(RESULT result QUERY WINDOWS_REGISTRY "HKLM")
  231. cmake_host_system_information(RESULT result QUERY WINDOWS_REGISTRY "HKLM/SOFTWARE/Kitware")
  232. cmake_host_system_information(RESULT result QUERY WINDOWS_REGISTRY "HKCU\\SOFTWARE\\Kitware")
  233. ``VALUE_NAMES``
  234. Request the list of value names defined under ``<key>``. If a default value
  235. is defined, it will be identified with the special name ``(default)``.
  236. ``SUBKEYS``
  237. Request the list of subkeys defined under ``<key>``.
  238. ``VALUE <name>``
  239. Request the data stored in value named ``<name>``. If ``VALUE`` is not
  240. specified or argument is the special name ``(default)``, the content of the
  241. default value, if any, will be returned.
  242. .. code-block:: cmake
  243. # query default value for HKLM/SOFTWARE/Kitware key
  244. cmake_host_system_information(RESULT result
  245. QUERY WINDOWS_REGISTRY "HKLM/SOFTWARE/Kitware")
  246. # query default value for HKLM/SOFTWARE/Kitware key using special value name
  247. cmake_host_system_information(RESULT result
  248. QUERY WINDOWS_REGISTRY "HKLM/SOFTWARE/Kitware"
  249. VALUE "(default)")
  250. Supported types are:
  251. * ``REG_SZ``.
  252. * ``REG_EXPAND_SZ``. The returned data is expanded.
  253. * ``REG_MULTI_SZ``. The returned is expressed as a CMake list. See also
  254. ``SEPARATOR`` sub-option.
  255. * ``REG_DWORD``.
  256. * ``REG_QWORD``.
  257. For all other types, an empty string is returned.
  258. ``VIEW``
  259. Specify which registry views must be queried. When not specified, ``BOTH``
  260. view is used.
  261. ``64``
  262. Query the 64bit registry. On ``32bit Windows``, returns always an empty
  263. string.
  264. ``32``
  265. Query the 32bit registry.
  266. ``64_32``
  267. For ``VALUE`` sub-option or default value, query the registry using view
  268. ``64``, and if the request failed, query the registry using view ``32``.
  269. For ``VALUE_NAMES`` and ``SUBKEYS`` sub-options, query both views (``64``
  270. and ``32``) and merge the results (sorted and duplicates removed).
  271. ``32_64``
  272. For ``VALUE`` sub-option or default value, query the registry using view
  273. ``32``, and if the request failed, query the registry using view ``64``.
  274. For ``VALUE_NAMES`` and ``SUBKEYS`` sub-options, query both views (``32``
  275. and ``64``) and merge the results (sorted and duplicates removed).
  276. ``HOST``
  277. Query the registry matching the architecture of the host: ``64`` on ``64bit
  278. Windows`` and ``32`` on ``32bit Windows``.
  279. ``TARGET``
  280. Query the registry matching the architecture specified by
  281. :variable:`CMAKE_SIZEOF_VOID_P` variable. If not defined, fallback to
  282. ``HOST`` view.
  283. ``BOTH``
  284. Query both views (``32`` and ``64``). The order depends of the following
  285. rules: If :variable:`CMAKE_SIZEOF_VOID_P` variable is defined. Use the
  286. following view depending of the content of this variable:
  287. * ``8``: ``64_32``
  288. * ``4``: ``32_64``
  289. If :variable:`CMAKE_SIZEOF_VOID_P` variable is not defined, rely on
  290. architecture of the host:
  291. * ``64bit``: ``64_32``
  292. * ``32bit``: ``32``
  293. ``SEPARATOR``
  294. Specify the separator character for ``REG_MULTI_SZ`` type. When not
  295. specified, the character ``\0`` is used.
  296. ``ERROR_VARIABLE <result>``
  297. Returns any error raised during query operation. In case of success, the
  298. variable holds an empty string.