CMakeDetermineSystem.cmake 7.2 KB

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