CMakeDetermineCompilerId.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #=============================================================================
  2. # Copyright 2007-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 distributed this file outside of CMake, substitute the full
  12. # License text for the above reference.)
  13. # Function to compile a source file to identify the compiler. This is
  14. # used internally by CMake and should not be included by user code.
  15. # If successful, sets CMAKE_<lang>_COMPILER_ID and CMAKE_<lang>_PLATFORM_ID
  16. FUNCTION(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
  17. # Make sure the compiler arguments are clean.
  18. STRING(STRIP "${CMAKE_${lang}_COMPILER_ARG1}" CMAKE_${lang}_COMPILER_ID_ARG1)
  19. # Make sure user-specified compiler flags are used.
  20. IF(CMAKE_${lang}_FLAGS)
  21. SET(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS})
  22. ELSE(CMAKE_${lang}_FLAGS)
  23. SET(CMAKE_${lang}_COMPILER_ID_FLAGS $ENV{${flagvar}})
  24. ENDIF(CMAKE_${lang}_FLAGS)
  25. STRING(REGEX REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}")
  26. # Compute the directory in which to run the test.
  27. SET(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CompilerId${lang})
  28. # Try building with no extra flags and then try each set
  29. # of helper flags. Stop when the compiler is identified.
  30. FOREACH(flags "" ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS})
  31. IF(NOT CMAKE_${lang}_COMPILER_ID)
  32. CMAKE_DETERMINE_COMPILER_ID_BUILD("${lang}" "${flags}" "${src}")
  33. FOREACH(file ${COMPILER_${lang}_PRODUCED_FILES})
  34. CMAKE_DETERMINE_COMPILER_ID_CHECK("${lang}" "${CMAKE_${lang}_COMPILER_ID_DIR}/${file}" "${src}")
  35. ENDFOREACH(file)
  36. ENDIF(NOT CMAKE_${lang}_COMPILER_ID)
  37. ENDFOREACH(flags)
  38. # if the format is unknown after all files have been checked, put "Unknown" in the cache
  39. IF(NOT CMAKE_EXECUTABLE_FORMAT)
  40. SET(CMAKE_EXECUTABLE_FORMAT "Unknown" CACHE INTERNAL "Executable file format")
  41. ENDIF(NOT CMAKE_EXECUTABLE_FORMAT)
  42. # Display the final identification result.
  43. IF(CMAKE_${lang}_COMPILER_ID)
  44. MESSAGE(STATUS "The ${lang} compiler identification is "
  45. "${CMAKE_${lang}_COMPILER_ID}")
  46. ELSE(CMAKE_${lang}_COMPILER_ID)
  47. MESSAGE(STATUS "The ${lang} compiler identification is unknown")
  48. ENDIF(CMAKE_${lang}_COMPILER_ID)
  49. SET(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  50. SET(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  51. SET(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  52. PARENT_SCOPE)
  53. ENDFUNCTION(CMAKE_DETERMINE_COMPILER_ID)
  54. #-----------------------------------------------------------------------------
  55. # Function to write the compiler id source file.
  56. FUNCTION(CMAKE_DETERMINE_COMPILER_ID_WRITE lang src)
  57. FILE(READ ${CMAKE_ROOT}/Modules/${src}.in ID_CONTENT_IN)
  58. STRING(CONFIGURE "${ID_CONTENT_IN}" ID_CONTENT_OUT @ONLY)
  59. FILE(WRITE ${CMAKE_${lang}_COMPILER_ID_DIR}/${src} "${ID_CONTENT_OUT}")
  60. ENDFUNCTION(CMAKE_DETERMINE_COMPILER_ID_WRITE)
  61. #-----------------------------------------------------------------------------
  62. # Function to build the compiler id source file and look for output
  63. # files.
  64. FUNCTION(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags src)
  65. # Create a clean working directory.
  66. FILE(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  67. FILE(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  68. CMAKE_DETERMINE_COMPILER_ID_WRITE("${lang}" "${src}")
  69. # Construct a description of this test case.
  70. SET(COMPILER_DESCRIPTION
  71. "Compiler: ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1}
  72. Build flags: ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  73. Id flags: ${testflags}
  74. ")
  75. # Compile the compiler identification source.
  76. IF(COMMAND EXECUTE_PROCESS)
  77. EXECUTE_PROCESS(
  78. COMMAND ${CMAKE_${lang}_COMPILER}
  79. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  80. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  81. ${testflags}
  82. "${src}"
  83. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  84. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  85. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  86. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  87. )
  88. ELSE(COMMAND EXECUTE_PROCESS)
  89. EXEC_PROGRAM(
  90. ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_DIR}
  91. ARGS ${CMAKE_${lang}_COMPILER_ID_ARG1}
  92. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  93. ${testflags}
  94. \"${src}\"
  95. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  96. RETURN_VALUE CMAKE_${lang}_COMPILER_ID_RESULT
  97. )
  98. ENDIF(COMMAND EXECUTE_PROCESS)
  99. # Check the result of compilation.
  100. IF(CMAKE_${lang}_COMPILER_ID_RESULT)
  101. # Compilation failed.
  102. SET(MSG
  103. "Compiling the ${lang} compiler identification source file \"${src}\" failed.
  104. ${COMPILER_DESCRIPTION}
  105. The output was:
  106. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  107. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  108. ")
  109. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log "${MSG}")
  110. #IF(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  111. # MESSAGE(FATAL_ERROR "${MSG}")
  112. #ENDIF(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  113. # No output files should be inspected.
  114. SET(COMPILER_${lang}_PRODUCED_FILES)
  115. ELSE(CMAKE_${lang}_COMPILER_ID_RESULT)
  116. # Compilation succeeded.
  117. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  118. "Compiling the ${lang} compiler identification source file \"${src}\" succeeded.
  119. ${COMPILER_DESCRIPTION}
  120. The output was:
  121. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  122. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  123. ")
  124. # Find the executable produced by the compiler, try all files in the
  125. # binary dir.
  126. FILE(GLOB COMPILER_${lang}_PRODUCED_FILES
  127. RELATIVE ${CMAKE_${lang}_COMPILER_ID_DIR}
  128. ${CMAKE_${lang}_COMPILER_ID_DIR}/*)
  129. LIST(REMOVE_ITEM COMPILER_${lang}_PRODUCED_FILES "${src}")
  130. FOREACH(file ${COMPILER_${lang}_PRODUCED_FILES})
  131. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  132. "Compilation of the ${lang} compiler identification source \""
  133. "${src}\" produced \"${file}\"\n\n")
  134. ENDFOREACH(file)
  135. IF(NOT COMPILER_${lang}_PRODUCED_FILES)
  136. # No executable was found.
  137. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  138. "Compilation of the ${lang} compiler identification source \""
  139. "${src}\" did not produce an executable in \""
  140. "${CMAKE_${lang}_COMPILER_ID_DIR}\".\n\n")
  141. ENDIF(NOT COMPILER_${lang}_PRODUCED_FILES)
  142. ENDIF(CMAKE_${lang}_COMPILER_ID_RESULT)
  143. # Return the files produced by the compilation.
  144. SET(COMPILER_${lang}_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE)
  145. ENDFUNCTION(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags src)
  146. #-----------------------------------------------------------------------------
  147. # Function to extract the compiler id from an executable.
  148. FUNCTION(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file)
  149. # Look for a compiler id if not yet known.
  150. IF(NOT CMAKE_${lang}_COMPILER_ID)
  151. # Read the compiler identification string from the executable file.
  152. SET(COMPILER_ID)
  153. SET(PLATFORM_ID)
  154. FILE(STRINGS ${file}
  155. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 3 REGEX "INFO:")
  156. SET(HAVE_COMPILER_TWICE 0)
  157. FOREACH(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  158. IF("${info}" MATCHES ".*INFO:compiler\\[([^]\"]*)\\].*")
  159. IF(COMPILER_ID)
  160. SET(COMPILER_ID_TWICE 1)
  161. ENDIF(COMPILER_ID)
  162. STRING(REGEX REPLACE ".*INFO:compiler\\[([^]]*)\\].*" "\\1"
  163. COMPILER_ID "${info}")
  164. ENDIF("${info}" MATCHES ".*INFO:compiler\\[([^]\"]*)\\].*")
  165. IF("${info}" MATCHES ".*INFO:platform\\[([^]\"]*)\\].*")
  166. STRING(REGEX REPLACE ".*INFO:platform\\[([^]]*)\\].*" "\\1"
  167. PLATFORM_ID "${info}")
  168. ENDIF("${info}" MATCHES ".*INFO:platform\\[([^]\"]*)\\].*")
  169. IF("${info}" MATCHES ".*INFO:arch\\[([^]\"]*)\\].*")
  170. STRING(REGEX REPLACE ".*INFO:arch\\[([^]]*)\\].*" "\\1"
  171. ARCHITECTURE_ID "${info}")
  172. ENDIF("${info}" MATCHES ".*INFO:arch\\[([^]\"]*)\\].*")
  173. ENDFOREACH(info)
  174. # Check if a valid compiler and platform were found.
  175. IF(COMPILER_ID AND NOT COMPILER_ID_TWICE)
  176. SET(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
  177. SET(CMAKE_${lang}_PLATFORM_ID "${PLATFORM_ID}")
  178. SET(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
  179. ENDIF(COMPILER_ID AND NOT COMPILER_ID_TWICE)
  180. # Check the compiler identification string.
  181. IF(CMAKE_${lang}_COMPILER_ID)
  182. # The compiler identification was found.
  183. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  184. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
  185. "${file}\"\n\n")
  186. ELSE(CMAKE_${lang}_COMPILER_ID)
  187. # The compiler identification could not be found.
  188. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  189. "The ${lang} compiler identification could not be found in \""
  190. "${file}\"\n\n")
  191. ENDIF(CMAKE_${lang}_COMPILER_ID)
  192. ENDIF(NOT CMAKE_${lang}_COMPILER_ID)
  193. # try to figure out the executable format: ELF, COFF, Mach-O
  194. IF(NOT CMAKE_EXECUTABLE_FORMAT)
  195. FILE(READ ${file} CMAKE_EXECUTABLE_MAGIC LIMIT 4 HEX)
  196. # ELF files start with 0x7f"ELF"
  197. IF("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
  198. SET(CMAKE_EXECUTABLE_FORMAT "ELF" CACHE INTERNAL "Executable file format")
  199. ENDIF("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
  200. # # COFF (.exe) files start with "MZ"
  201. # IF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
  202. # SET(CMAKE_EXECUTABLE_FORMAT "COFF" CACHE STRING "Executable file format")
  203. # ENDIF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
  204. #
  205. # # Mach-O files start with CAFEBABE or FEEDFACE, according to http://radio.weblogs.com/0100490/2003/01/28.html
  206. # IF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe")
  207. # SET(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  208. # ENDIF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe")
  209. # IF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface")
  210. # SET(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  211. # ENDIF("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface")
  212. ENDIF(NOT CMAKE_EXECUTABLE_FORMAT)
  213. # Return the information extracted.
  214. SET(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  215. SET(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  216. SET(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  217. PARENT_SCOPE)
  218. SET(CMAKE_EXECUTABLE_FORMAT "${CMAKE_EXECUTABLE_FORMAT}" PARENT_SCOPE)
  219. ENDFUNCTION(CMAKE_DETERMINE_COMPILER_ID_CHECK lang)