CMakeDetermineSystem.cmake 7.7 KB

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