CMakeDetermineCompilerId.cmake 13 KB

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