CMakeDetermineSystem.cmake 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This module is used by the Makefile generator to determine the following variables:
  4. # CMAKE_SYSTEM_NAME - on unix this is uname -s, for windows it is Windows
  5. # CMAKE_SYSTEM_VERSION - on unix this is uname -r, for windows it is empty
  6. # CMAKE_SYSTEM - ${CMAKE_SYSTEM}-${CMAKE_SYSTEM_VERSION}, for windows: ${CMAKE_SYSTEM}
  7. #
  8. # Expected uname -s output:
  9. #
  10. # AIX AIX
  11. # BSD/OS BSD/OS
  12. # FreeBSD FreeBSD
  13. # HP-UX HP-UX
  14. # Linux Linux
  15. # GNU/kFreeBSD GNU/kFreeBSD
  16. # NetBSD NetBSD
  17. # OpenBSD OpenBSD
  18. # OFS/1 (Digital Unix) OSF1
  19. # SCO OpenServer 5 SCO_SV
  20. # SCO UnixWare 7 UnixWare
  21. # SCO UnixWare (pre release 7) UNIX_SV
  22. # SCO XENIX Xenix
  23. # Solaris SunOS
  24. # SunOS SunOS
  25. # Tru64 Tru64
  26. # Ultrix ULTRIX
  27. # cygwin CYGWIN_NT-5.1
  28. # MacOSX Darwin
  29. # find out on which system cmake runs
  30. if(CMAKE_HOST_UNIX)
  31. find_program(CMAKE_UNAME uname /bin /usr/bin /usr/local/bin )
  32. if(CMAKE_UNAME)
  33. if(CMAKE_HOST_SYSTEM_NAME STREQUAL "AIX")
  34. exec_program(${CMAKE_UNAME} ARGS -v OUTPUT_VARIABLE _CMAKE_HOST_SYSTEM_MAJOR_VERSION)
  35. exec_program(${CMAKE_UNAME} ARGS -r OUTPUT_VARIABLE _CMAKE_HOST_SYSTEM_MINOR_VERSION)
  36. set(CMAKE_HOST_SYSTEM_VERSION "${_CMAKE_HOST_SYSTEM_MAJOR_VERSION}.${_CMAKE_HOST_SYSTEM_MINOR_VERSION}")
  37. unset(_CMAKE_HOST_SYSTEM_MAJOR_VERSION)
  38. unset(_CMAKE_HOST_SYSTEM_MINOR_VERSION)
  39. else()
  40. exec_program(${CMAKE_UNAME} ARGS -r OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION)
  41. endif()
  42. if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|CYGWIN.*|^GNU$|Android")
  43. exec_program(${CMAKE_UNAME} ARGS -m OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
  44. RETURN_VALUE val)
  45. elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
  46. # If we are running on Apple Silicon, honor CMAKE_APPLE_SILICON_PROCESSOR.
  47. if(DEFINED CMAKE_APPLE_SILICON_PROCESSOR)
  48. set(_CMAKE_APPLE_SILICON_PROCESSOR "${CMAKE_APPLE_SILICON_PROCESSOR}")
  49. elseif(DEFINED ENV{CMAKE_APPLE_SILICON_PROCESSOR})
  50. set(_CMAKE_APPLE_SILICON_PROCESSOR "$ENV{CMAKE_APPLE_SILICON_PROCESSOR}")
  51. else()
  52. set(_CMAKE_APPLE_SILICON_PROCESSOR "")
  53. endif()
  54. if(_CMAKE_APPLE_SILICON_PROCESSOR)
  55. if(";${_CMAKE_APPLE_SILICON_PROCESSOR};" MATCHES "^;(arm64|x86_64);$")
  56. execute_process(COMMAND sysctl -q hw.optional.arm64
  57. OUTPUT_VARIABLE _sysctl_stdout
  58. ERROR_VARIABLE _sysctl_stderr
  59. RESULT_VARIABLE _sysctl_result
  60. )
  61. if(NOT _sysctl_result EQUAL 0 OR NOT _sysctl_stdout MATCHES "hw.optional.arm64: 1")
  62. set(_CMAKE_APPLE_SILICON_PROCESSOR "")
  63. endif()
  64. unset(_sysctl_result)
  65. unset(_sysctl_stderr)
  66. unset(_sysctl_stdout)
  67. endif()
  68. endif()
  69. if(_CMAKE_APPLE_SILICON_PROCESSOR)
  70. set(CMAKE_HOST_SYSTEM_PROCESSOR "${_CMAKE_APPLE_SILICON_PROCESSOR}")
  71. else()
  72. exec_program(${CMAKE_UNAME} ARGS -m OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
  73. RETURN_VALUE val)
  74. endif()
  75. unset(_CMAKE_APPLE_SILICON_PROCESSOR)
  76. if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "Power Macintosh")
  77. # OS X ppc 'uname -m' may report 'Power Macintosh' instead of 'powerpc'
  78. set(CMAKE_HOST_SYSTEM_PROCESSOR "powerpc")
  79. endif()
  80. elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "OpenBSD")
  81. exec_program(arch ARGS -s OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
  82. RETURN_VALUE val)
  83. else()
  84. exec_program(${CMAKE_UNAME} ARGS -p OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
  85. RETURN_VALUE val)
  86. if("${val}" GREATER 0)
  87. exec_program(${CMAKE_UNAME} ARGS -m OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
  88. RETURN_VALUE val)
  89. endif()
  90. endif()
  91. # check the return of the last uname -m or -p
  92. if("${val}" GREATER 0)
  93. set(CMAKE_HOST_SYSTEM_PROCESSOR "unknown")
  94. endif()
  95. set(CMAKE_UNAME ${CMAKE_UNAME} CACHE INTERNAL "uname command")
  96. # processor may have double quote in the name, and that needs to be removed
  97. string(REPLACE "\"" "" CMAKE_HOST_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
  98. string(REPLACE "/" "_" CMAKE_HOST_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
  99. endif()
  100. else()
  101. if(CMAKE_HOST_WIN32)
  102. if (DEFINED ENV{PROCESSOR_ARCHITEW6432})
  103. set (CMAKE_HOST_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITEW6432}")
  104. else()
  105. set (CMAKE_HOST_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITECTURE}")
  106. endif()
  107. endif()
  108. endif()
  109. # if a toolchain file is used, the user wants to cross compile.
  110. # in this case read the toolchain file and keep the CMAKE_HOST_SYSTEM_*
  111. # variables around so they can be used in CMakeLists.txt.
  112. # In all other cases, the host and target platform are the same.
  113. if(CMAKE_TOOLCHAIN_FILE)
  114. # at first try to load it as path relative to the directory from which cmake has been run
  115. include("${CMAKE_BINARY_DIR}/${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE)
  116. if(NOT _INCLUDED_TOOLCHAIN_FILE)
  117. # if the file isn't found there, check the default locations
  118. include("${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE)
  119. endif()
  120. if(_INCLUDED_TOOLCHAIN_FILE)
  121. set(CMAKE_TOOLCHAIN_FILE "${_INCLUDED_TOOLCHAIN_FILE}" CACHE FILEPATH "The CMake toolchain file" FORCE)
  122. else()
  123. message(FATAL_ERROR "Could not find toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
  124. set(CMAKE_TOOLCHAIN_FILE "NOTFOUND" CACHE FILEPATH "The CMake toolchain file" FORCE)
  125. endif()
  126. endif()
  127. # if CMAKE_SYSTEM_NAME is here already set, either it comes from a toolchain file
  128. # or it was set via -DCMAKE_SYSTEM_NAME=...
  129. # if that's the case, assume we are crosscompiling
  130. if(CMAKE_SYSTEM_NAME)
  131. if(NOT DEFINED CMAKE_CROSSCOMPILING)
  132. set(CMAKE_CROSSCOMPILING TRUE)
  133. endif()
  134. set(PRESET_CMAKE_SYSTEM_NAME TRUE)
  135. elseif(CMAKE_VS_WINCE_VERSION)
  136. set(CMAKE_SYSTEM_NAME "WindowsCE")
  137. set(CMAKE_SYSTEM_VERSION "${CMAKE_VS_WINCE_VERSION}")
  138. set(CMAKE_SYSTEM_PROCESSOR "${MSVC_C_ARCHITECTURE_ID}")
  139. set(CMAKE_CROSSCOMPILING TRUE)
  140. set(PRESET_CMAKE_SYSTEM_NAME TRUE)
  141. else()
  142. set(CMAKE_SYSTEM_NAME "${CMAKE_HOST_SYSTEM_NAME}")
  143. if(NOT DEFINED CMAKE_SYSTEM_VERSION)
  144. set(CMAKE_SYSTEM_VERSION "${CMAKE_HOST_SYSTEM_VERSION}")
  145. endif()
  146. set(CMAKE_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
  147. set(CMAKE_CROSSCOMPILING FALSE)
  148. set(PRESET_CMAKE_SYSTEM_NAME FALSE)
  149. endif()
  150. include(Platform/${CMAKE_SYSTEM_NAME}-Determine OPTIONAL)
  151. macro(ADJUST_CMAKE_SYSTEM_VARIABLES _PREFIX)
  152. if(NOT ${_PREFIX}_NAME)
  153. set(${_PREFIX}_NAME "UnknownOS")
  154. endif()
  155. # fix for BSD/OS , remove the /
  156. if(${_PREFIX}_NAME MATCHES BSD.OS)
  157. set(${_PREFIX}_NAME BSDOS)
  158. endif()
  159. # fix for GNU/kFreeBSD, remove the GNU/
  160. if(${_PREFIX}_NAME MATCHES kFreeBSD)
  161. set(${_PREFIX}_NAME kFreeBSD)
  162. endif()
  163. # fix for CYGWIN which has windows version in it
  164. if(${_PREFIX}_NAME MATCHES CYGWIN)
  165. set(${_PREFIX}_NAME CYGWIN)
  166. endif()
  167. # set CMAKE_SYSTEM to the CMAKE_SYSTEM_NAME
  168. set(${_PREFIX} ${${_PREFIX}_NAME})
  169. # if there is a CMAKE_SYSTEM_VERSION then add a -${CMAKE_SYSTEM_VERSION}
  170. if(${_PREFIX}_VERSION)
  171. set(${_PREFIX} ${${_PREFIX}}-${${_PREFIX}_VERSION})
  172. endif()
  173. endmacro()
  174. ADJUST_CMAKE_SYSTEM_VARIABLES(CMAKE_SYSTEM)
  175. ADJUST_CMAKE_SYSTEM_VARIABLES(CMAKE_HOST_SYSTEM)
  176. # this file is also executed from cpack, then we don't need to generate these files
  177. # in this case there is no CMAKE_BINARY_DIR
  178. if(CMAKE_BINARY_DIR)
  179. # write entry to the log file
  180. if(PRESET_CMAKE_SYSTEM_NAME)
  181. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  182. "The target system is: ${CMAKE_SYSTEM_NAME} - ${CMAKE_SYSTEM_VERSION} - ${CMAKE_SYSTEM_PROCESSOR}\n")
  183. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  184. "The host system is: ${CMAKE_HOST_SYSTEM_NAME} - ${CMAKE_HOST_SYSTEM_VERSION} - ${CMAKE_HOST_SYSTEM_PROCESSOR}\n")
  185. else()
  186. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  187. "The system is: ${CMAKE_SYSTEM_NAME} - ${CMAKE_SYSTEM_VERSION} - ${CMAKE_SYSTEM_PROCESSOR}\n")
  188. endif()
  189. # if a toolchain file is used, it needs to be included in the configured file,
  190. # so settings done there are also available if they don't go in the cache and in try_compile()
  191. set(INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED)
  192. if(CMAKE_TOOLCHAIN_FILE)
  193. set(INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED "include(\"${CMAKE_TOOLCHAIN_FILE}\")")
  194. endif()
  195. # configure variables set in this file for fast reload, the template file is defined at the top of this file
  196. configure_file(${CMAKE_ROOT}/Modules/CMakeSystem.cmake.in
  197. ${CMAKE_PLATFORM_INFO_DIR}/CMakeSystem.cmake
  198. @ONLY)
  199. endif()