CMakeDetermineCompilerId.cmake 13 KB

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