CMakeDetermineCompilerId.cmake 13 KB

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