CMakeDetermineSystem.cmake 7.7 KB

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