CMakeDetermineSystem.cmake 7.6 KB

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