CMakeDetermineSystem.cmake 7.0 KB

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