CMakeDetermineCompilerId.cmake 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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(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_PLATFORM_INFO_DIR}/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(CMAKE_${lang}_COMPILER AND NOT CMAKE_${lang}_COMPILER_ID)
  41. CMAKE_DETERMINE_COMPILER_ID_VENDOR(${lang})
  42. endif()
  43. if (COMPILER_QNXNTO AND CMAKE_${lang}_COMPILER_ID STREQUAL GNU)
  44. execute_process(
  45. COMMAND "${CMAKE_${lang}_COMPILER}"
  46. -V
  47. OUTPUT_VARIABLE output ERROR_VARIABLE output
  48. RESULT_VARIABLE result
  49. TIMEOUT 10
  50. )
  51. if (output MATCHES "targets available")
  52. set(CMAKE_${lang}_COMPILER_ID QCC)
  53. # http://community.qnx.com/sf/discussion/do/listPosts/projects.community/discussion.qnx_momentics_community_support.topc3555?_pagenum=2
  54. # The qcc driver does not itself have a version.
  55. endif()
  56. endif()
  57. # if the format is unknown after all files have been checked, put "Unknown" in the cache
  58. if(NOT CMAKE_EXECUTABLE_FORMAT)
  59. set(CMAKE_EXECUTABLE_FORMAT "Unknown" CACHE INTERNAL "Executable file format")
  60. endif()
  61. # Display the final identification result.
  62. if(CMAKE_${lang}_COMPILER_ID)
  63. if(CMAKE_${lang}_COMPILER_VERSION)
  64. set(_version " ${CMAKE_${lang}_COMPILER_VERSION}")
  65. else()
  66. set(_version "")
  67. endif()
  68. message(STATUS "The ${lang} compiler identification is "
  69. "${CMAKE_${lang}_COMPILER_ID}${_version}")
  70. else()
  71. message(STATUS "The ${lang} compiler identification is unknown")
  72. endif()
  73. # Check if compiler id detection gave us the compiler tool.
  74. if(CMAKE_${lang}_COMPILER_ID_TOOL)
  75. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER_ID_TOOL}" PARENT_SCOPE)
  76. elseif(NOT CMAKE_${lang}_COMPILER)
  77. set(CMAKE_${lang}_COMPILER "CMAKE_${lang}_COMPILER-NOTFOUND" PARENT_SCOPE)
  78. endif()
  79. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  80. set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  81. set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  82. PARENT_SCOPE)
  83. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
  84. set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE)
  85. set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE)
  86. endfunction()
  87. include(CMakeCompilerIdDetection)
  88. #-----------------------------------------------------------------------------
  89. # Function to write the compiler id source file.
  90. function(CMAKE_DETERMINE_COMPILER_ID_WRITE lang src)
  91. find_file(src_in ${src}.in PATHS ${CMAKE_ROOT}/Modules ${CMAKE_MODULE_PATH} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
  92. file(READ ${src_in} ID_CONTENT_IN)
  93. compiler_id_detection(CMAKE_${lang}_COMPILER_ID_CONTENT ${lang}
  94. ID_STRING
  95. VERSION_STRINGS
  96. PLATFORM_DEFAULT_COMPILER
  97. )
  98. unset(src_in CACHE)
  99. string(CONFIGURE "${ID_CONTENT_IN}" ID_CONTENT_OUT @ONLY)
  100. file(WRITE ${CMAKE_${lang}_COMPILER_ID_DIR}/${src} "${ID_CONTENT_OUT}")
  101. endfunction()
  102. #-----------------------------------------------------------------------------
  103. # Function to build the compiler id source file and look for output
  104. # files.
  105. function(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags src)
  106. # Create a clean working directory.
  107. file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  108. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  109. CMAKE_DETERMINE_COMPILER_ID_WRITE("${lang}" "${src}")
  110. # Construct a description of this test case.
  111. set(COMPILER_DESCRIPTION
  112. "Compiler: ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1}
  113. Build flags: ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  114. Id flags: ${testflags}
  115. ")
  116. # Compile the compiler identification source.
  117. if(CMAKE_GENERATOR STREQUAL "Visual Studio 6" AND
  118. lang STREQUAL "Fortran")
  119. set(CMAKE_${lang}_COMPILER_ID_RESULT 1)
  120. set(CMAKE_${lang}_COMPILER_ID_OUTPUT "No Intel Fortran in VS 6")
  121. elseif("${CMAKE_GENERATOR}" MATCHES "Visual Studio ([0-9]+)")
  122. set(vs_version ${CMAKE_MATCH_1})
  123. set(id_platform ${CMAKE_VS_PLATFORM_NAME})
  124. set(id_lang "${lang}")
  125. set(id_cl cl.exe)
  126. if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
  127. set(v NsightTegra)
  128. set(ext vcxproj)
  129. if(lang STREQUAL CXX)
  130. set(id_gcc g++)
  131. set(id_clang clang++)
  132. else()
  133. set(id_gcc gcc)
  134. set(id_clang clang)
  135. endif()
  136. elseif(lang STREQUAL Fortran)
  137. set(v Intel)
  138. set(ext vfproj)
  139. set(id_cl ifort.exe)
  140. elseif(NOT "${vs_version}" VERSION_LESS 10)
  141. set(v 10)
  142. set(ext vcxproj)
  143. elseif(NOT "${vs_version}" VERSION_LESS 7)
  144. set(id_version ${vs_version}.00)
  145. set(v 7)
  146. set(ext vcproj)
  147. else()
  148. set(v 6)
  149. set(ext dsp)
  150. endif()
  151. if("${id_platform}" STREQUAL "Itanium")
  152. set(id_platform ia64)
  153. endif()
  154. if(CMAKE_VS_PLATFORM_TOOLSET)
  155. if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
  156. set(id_toolset "<NdkToolchainVersion>${CMAKE_VS_PLATFORM_TOOLSET}</NdkToolchainVersion>")
  157. else()
  158. set(id_toolset "<PlatformToolset>${CMAKE_VS_PLATFORM_TOOLSET}</PlatformToolset>")
  159. if(CMAKE_VS_PLATFORM_TOOLSET MATCHES "Intel")
  160. set(id_cl icl.exe)
  161. endif()
  162. endif()
  163. else()
  164. set(id_toolset "")
  165. endif()
  166. if(CMAKE_SYSTEM_NAME STREQUAL "WindowsPhone")
  167. set(id_system "<ApplicationType>Windows Phone</ApplicationType>")
  168. elseif(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
  169. set(id_system "<ApplicationType>Windows Store</ApplicationType>")
  170. else()
  171. set(id_system "")
  172. endif()
  173. if(id_system AND CMAKE_SYSTEM_VERSION)
  174. set(id_system_version "<ApplicationTypeRevision>${CMAKE_SYSTEM_VERSION}</ApplicationTypeRevision>")
  175. else()
  176. set(id_system_version "")
  177. endif()
  178. if(id_platform STREQUAL ARM)
  179. set(id_WindowsSDKDesktopARMSupport "<WindowsSDKDesktopARMSupport>true</WindowsSDKDesktopARMSupport>")
  180. else()
  181. set(id_WindowsSDKDesktopARMSupport "")
  182. endif()
  183. if(CMAKE_VS_WINCE_VERSION)
  184. set(id_entrypoint "mainACRTStartup")
  185. if("${vs_version}" VERSION_LESS 9)
  186. set(id_subsystem 9)
  187. else()
  188. set(id_subsystem 8)
  189. endif()
  190. else()
  191. set(id_subsystem 1)
  192. endif()
  193. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  194. get_filename_component(id_src "${src}" NAME)
  195. configure_file(${CMAKE_ROOT}/Modules/CompilerId/VS-${v}.${ext}.in
  196. ${id_dir}/CompilerId${lang}.${ext} @ONLY)
  197. if(CMAKE_VS_MSBUILD_COMMAND AND NOT lang STREQUAL "Fortran")
  198. set(command "${CMAKE_VS_MSBUILD_COMMAND}" "CompilerId${lang}.${ext}"
  199. "/p:Configuration=Debug" "/p:Platform=${id_platform}" "/p:VisualStudioVersion=${vs_version}.0"
  200. )
  201. elseif(CMAKE_VS_DEVENV_COMMAND)
  202. set(command "${CMAKE_VS_DEVENV_COMMAND}" "CompilerId${lang}.${ext}" "/build" "Debug")
  203. elseif(CMAKE_VS_MSDEV_COMMAND)
  204. set(command "${CMAKE_VS_MSDEV_COMMAND}" "CompilerId${lang}.${ext}" "/make")
  205. else()
  206. set(command "")
  207. endif()
  208. if(command)
  209. execute_process(
  210. COMMAND ${command}
  211. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  212. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  213. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  214. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  215. )
  216. else()
  217. set(CMAKE_${lang}_COMPILER_ID_RESULT 1)
  218. set(CMAKE_${lang}_COMPILER_ID_OUTPUT "VS environment not known to support ${lang}")
  219. endif()
  220. # Match the compiler location line printed out.
  221. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "CMAKE_${lang}_COMPILER=([^%\r\n]+)[\r\n]")
  222. # Strip VS diagnostic output from the end of the line.
  223. string(REGEX REPLACE " \\(TaskId:[0-9]*\\)$" "" _comp "${CMAKE_MATCH_1}")
  224. if(EXISTS "${_comp}")
  225. file(TO_CMAKE_PATH "${_comp}" _comp)
  226. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  227. endif()
  228. endif()
  229. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  230. set(id_lang "${lang}")
  231. set(id_type ${CMAKE_${lang}_COMPILER_XCODE_TYPE})
  232. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  233. get_filename_component(id_src "${src}" NAME)
  234. if(CMAKE_XCODE_PLATFORM_TOOLSET)
  235. set(id_toolset "GCC_VERSION = ${CMAKE_XCODE_PLATFORM_TOOLSET};")
  236. else()
  237. set(id_toolset "")
  238. endif()
  239. if(CMAKE_OSX_DEPLOYMENT_TARGET)
  240. set(id_deployment_target
  241. "MACOSX_DEPLOYMENT_TARGET = \"${CMAKE_OSX_DEPLOYMENT_TARGET}\";")
  242. else()
  243. set(id_deployment_target "")
  244. endif()
  245. set(id_product_type "com.apple.product-type.tool")
  246. if(CMAKE_OSX_SYSROOT)
  247. set(id_sdkroot "SDKROOT = \"${CMAKE_OSX_SYSROOT}\";")
  248. if(CMAKE_OSX_SYSROOT MATCHES "(^|/)[Ii][Pp][Hh][Oo][Nn][Ee]")
  249. set(id_product_type "com.apple.product-type.bundle.unit-test")
  250. endif()
  251. else()
  252. set(id_sdkroot "")
  253. endif()
  254. if(CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY)
  255. set(id_code_sign_identity "CODE_SIGN_IDENTITY = \"${CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY}\";")
  256. else()
  257. set(id_code_sign_identity "")
  258. endif()
  259. if(NOT ${XCODE_VERSION} VERSION_LESS 3)
  260. set(v 3)
  261. set(ext xcodeproj)
  262. elseif(NOT ${XCODE_VERSION} VERSION_LESS 2)
  263. set(v 2)
  264. set(ext xcodeproj)
  265. else()
  266. set(v 1)
  267. set(ext xcode)
  268. endif()
  269. configure_file(${CMAKE_ROOT}/Modules/CompilerId/Xcode-${v}.pbxproj.in
  270. ${id_dir}/CompilerId${lang}.${ext}/project.pbxproj @ONLY)
  271. unset(_ENV_MACOSX_DEPLOYMENT_TARGET)
  272. if(DEFINED ENV{MACOSX_DEPLOYMENT_TARGET})
  273. set(_ENV_MACOSX_DEPLOYMENT_TARGET "$ENV{MACOSX_DEPLOYMENT_TARGET}")
  274. set(ENV{MACOSX_DEPLOYMENT_TARGET} "")
  275. endif()
  276. execute_process(COMMAND xcodebuild
  277. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  278. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  279. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  280. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  281. )
  282. if(DEFINED _ENV_MACOSX_DEPLOYMENT_TARGET)
  283. set(ENV{MACOSX_DEPLOYMENT_TARGET} "${_ENV_MACOSX_DEPLOYMENT_TARGET}")
  284. endif()
  285. # Match the link line from xcodebuild output of the form
  286. # Ld ...
  287. # ...
  288. # /path/to/cc ...CompilerId${lang}/...
  289. # to extract the compiler front-end for the language.
  290. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "\nLd[^\n]*(\n[ \t]+[^\n]*)*\n[ \t]+([^ \t\r\n]+)[^\r\n]*-o[^\r\n]*CompilerId${lang}/(\\./)?(CompilerId${lang}.xctest/)?CompilerId${lang}[ \t\n\\\"]")
  291. set(_comp "${CMAKE_MATCH_2}")
  292. if(EXISTS "${_comp}")
  293. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  294. endif()
  295. endif()
  296. else()
  297. if(COMMAND EXECUTE_PROCESS)
  298. execute_process(
  299. COMMAND "${CMAKE_${lang}_COMPILER}"
  300. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  301. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  302. ${testflags}
  303. "${src}"
  304. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  305. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  306. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  307. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  308. )
  309. else()
  310. exec_program(
  311. "${CMAKE_${lang}_COMPILER}" ${CMAKE_${lang}_COMPILER_ID_DIR}
  312. ARGS ${CMAKE_${lang}_COMPILER_ID_ARG1}
  313. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  314. ${testflags}
  315. \"${src}\"
  316. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  317. RETURN_VALUE CMAKE_${lang}_COMPILER_ID_RESULT
  318. )
  319. endif()
  320. endif()
  321. # Check the result of compilation.
  322. if(CMAKE_${lang}_COMPILER_ID_RESULT
  323. # Intel Fortran warns and ignores preprocessor lines without /fpp
  324. OR CMAKE_${lang}_COMPILER_ID_OUTPUT MATCHES "Bad # preprocessor line"
  325. )
  326. # Compilation failed.
  327. set(MSG
  328. "Compiling the ${lang} compiler identification source file \"${src}\" failed.
  329. ${COMPILER_DESCRIPTION}
  330. The output was:
  331. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  332. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  333. ")
  334. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log "${MSG}")
  335. #if(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  336. # message(FATAL_ERROR "${MSG}")
  337. #endif()
  338. # No output files should be inspected.
  339. set(COMPILER_${lang}_PRODUCED_FILES)
  340. else()
  341. # Compilation succeeded.
  342. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  343. "Compiling the ${lang} compiler identification source file \"${src}\" succeeded.
  344. ${COMPILER_DESCRIPTION}
  345. The output was:
  346. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  347. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  348. ")
  349. # Find the executable produced by the compiler, try all files in the
  350. # binary dir.
  351. file(GLOB files
  352. RELATIVE ${CMAKE_${lang}_COMPILER_ID_DIR}
  353. # normal case
  354. ${CMAKE_${lang}_COMPILER_ID_DIR}/*
  355. # com.apple.package-type.bundle.unit-test
  356. ${CMAKE_${lang}_COMPILER_ID_DIR}/*.xctest/*
  357. )
  358. list(REMOVE_ITEM files "${src}")
  359. set(COMPILER_${lang}_PRODUCED_FILES "")
  360. foreach(file ${files})
  361. if(NOT IS_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}/${file})
  362. list(APPEND COMPILER_${lang}_PRODUCED_FILES ${file})
  363. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  364. "Compilation of the ${lang} compiler identification source \""
  365. "${src}\" produced \"${file}\"\n\n")
  366. endif()
  367. endforeach()
  368. if(NOT COMPILER_${lang}_PRODUCED_FILES)
  369. # No executable was found.
  370. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  371. "Compilation of the ${lang} compiler identification source \""
  372. "${src}\" did not produce an executable in \""
  373. "${CMAKE_${lang}_COMPILER_ID_DIR}\".\n\n")
  374. endif()
  375. endif()
  376. # Return the files produced by the compilation.
  377. set(COMPILER_${lang}_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE)
  378. endfunction()
  379. #-----------------------------------------------------------------------------
  380. # Function to extract the compiler id from an executable.
  381. function(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file)
  382. # Look for a compiler id if not yet known.
  383. if(NOT CMAKE_${lang}_COMPILER_ID)
  384. # Read the compiler identification string from the executable file.
  385. set(COMPILER_ID)
  386. set(COMPILER_VERSION)
  387. set(PLATFORM_ID)
  388. set(ARCHITECTURE_ID)
  389. set(SIMULATE_ID)
  390. set(SIMULATE_VERSION)
  391. file(STRINGS ${file}
  392. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 6 REGEX "INFO:[A-Za-z0-9_]+\\[[^]]*\\]")
  393. set(COMPILER_ID_TWICE)
  394. foreach(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  395. if("${info}" MATCHES "INFO:compiler\\[([^]\"]*)\\]")
  396. if(COMPILER_ID)
  397. set(COMPILER_ID_TWICE 1)
  398. endif()
  399. set(COMPILER_ID "${CMAKE_MATCH_1}")
  400. endif()
  401. if("${info}" MATCHES "INFO:platform\\[([^]\"]*)\\]")
  402. set(PLATFORM_ID "${CMAKE_MATCH_1}")
  403. endif()
  404. if("${info}" MATCHES "INFO:arch\\[([^]\"]*)\\]")
  405. set(ARCHITECTURE_ID "${CMAKE_MATCH_1}")
  406. endif()
  407. if("${info}" MATCHES "INFO:compiler_version\\[([^]\"]*)\\]")
  408. string(REGEX REPLACE "^0+([0-9])" "\\1" COMPILER_VERSION "${CMAKE_MATCH_1}")
  409. string(REGEX REPLACE "\\.0+([0-9])" ".\\1" COMPILER_VERSION "${COMPILER_VERSION}")
  410. endif()
  411. if("${info}" MATCHES "INFO:simulate\\[([^]\"]*)\\]")
  412. set(SIMULATE_ID "${CMAKE_MATCH_1}")
  413. endif()
  414. if("${info}" MATCHES "INFO:simulate_version\\[([^]\"]*)\\]")
  415. string(REGEX REPLACE "^0+([0-9])" "\\1" SIMULATE_VERSION "${CMAKE_MATCH_1}")
  416. string(REGEX REPLACE "\\.0+([0-9])" ".\\1" SIMULATE_VERSION "${SIMULATE_VERSION}")
  417. endif()
  418. if("${info}" MATCHES "INFO:qnxnto\\[\\]")
  419. set(COMPILER_QNXNTO 1)
  420. endif()
  421. endforeach()
  422. # Detect the exact architecture from the PE header.
  423. if(WIN32)
  424. # The offset to the PE signature is stored at 0x3c.
  425. file(READ ${file} peoffsethex LIMIT 1 OFFSET 60 HEX)
  426. string(SUBSTRING "${peoffsethex}" 0 1 peoffsethex1)
  427. string(SUBSTRING "${peoffsethex}" 1 1 peoffsethex2)
  428. set(peoffsetexpression "${peoffsethex1} * 16 + ${peoffsethex2}")
  429. string(REPLACE "a" "10" peoffsetexpression "${peoffsetexpression}")
  430. string(REPLACE "b" "11" peoffsetexpression "${peoffsetexpression}")
  431. string(REPLACE "c" "12" peoffsetexpression "${peoffsetexpression}")
  432. string(REPLACE "d" "13" peoffsetexpression "${peoffsetexpression}")
  433. string(REPLACE "e" "14" peoffsetexpression "${peoffsetexpression}")
  434. string(REPLACE "f" "15" peoffsetexpression "${peoffsetexpression}")
  435. math(EXPR peoffset "${peoffsetexpression}")
  436. file(READ ${file} peheader LIMIT 6 OFFSET ${peoffset} HEX)
  437. if(peheader STREQUAL "50450000a201")
  438. set(ARCHITECTURE_ID "SH3")
  439. elseif(peheader STREQUAL "50450000a301")
  440. set(ARCHITECTURE_ID "SH3DSP")
  441. elseif(peheader STREQUAL "50450000a601")
  442. set(ARCHITECTURE_ID "SH4")
  443. elseif(peheader STREQUAL "50450000a801")
  444. set(ARCHITECTURE_ID "SH5")
  445. elseif(peheader STREQUAL "50450000c201")
  446. set(ARCHITECTURE_ID "THUMB")
  447. endif()
  448. endif()
  449. # Check if a valid compiler and platform were found.
  450. if(COMPILER_ID AND NOT COMPILER_ID_TWICE)
  451. set(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
  452. set(CMAKE_${lang}_PLATFORM_ID "${PLATFORM_ID}")
  453. set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
  454. set(CMAKE_${lang}_COMPILER_VERSION "${COMPILER_VERSION}")
  455. set(CMAKE_${lang}_SIMULATE_ID "${SIMULATE_ID}")
  456. set(CMAKE_${lang}_SIMULATE_VERSION "${SIMULATE_VERSION}")
  457. endif()
  458. # Check the compiler identification string.
  459. if(CMAKE_${lang}_COMPILER_ID)
  460. # The compiler identification was found.
  461. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  462. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
  463. "${file}\"\n\n")
  464. else()
  465. # The compiler identification could not be found.
  466. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  467. "The ${lang} compiler identification could not be found in \""
  468. "${file}\"\n\n")
  469. endif()
  470. endif()
  471. # try to figure out the executable format: ELF, COFF, Mach-O
  472. if(NOT CMAKE_EXECUTABLE_FORMAT)
  473. file(READ ${file} CMAKE_EXECUTABLE_MAGIC LIMIT 4 HEX)
  474. # ELF files start with 0x7f"ELF"
  475. if("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
  476. set(CMAKE_EXECUTABLE_FORMAT "ELF" CACHE INTERNAL "Executable file format")
  477. endif()
  478. # # COFF (.exe) files start with "MZ"
  479. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
  480. # set(CMAKE_EXECUTABLE_FORMAT "COFF" CACHE STRING "Executable file format")
  481. # endif()
  482. #
  483. # # Mach-O files start with CAFEBABE or FEEDFACE, according to http://radio.weblogs.com/0100490/2003/01/28.html
  484. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe")
  485. # set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  486. # endif()
  487. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface")
  488. # set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  489. # endif()
  490. endif()
  491. if(NOT DEFINED CMAKE_EXECUTABLE_FORMAT)
  492. set(CMAKE_EXECUTABLE_FORMAT)
  493. endif()
  494. # Return the information extracted.
  495. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  496. set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  497. set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  498. PARENT_SCOPE)
  499. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
  500. set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE)
  501. set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE)
  502. set(CMAKE_EXECUTABLE_FORMAT "${CMAKE_EXECUTABLE_FORMAT}" PARENT_SCOPE)
  503. set(COMPILER_QNXNTO "${COMPILER_QNXNTO}" PARENT_SCOPE)
  504. endfunction()
  505. #-----------------------------------------------------------------------------
  506. # Function to query the compiler vendor.
  507. # This uses a table with entries of the form
  508. # list(APPEND CMAKE_${lang}_COMPILER_ID_VENDORS ${vendor})
  509. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor} -some-vendor-flag)
  510. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor} "Some Vendor Output")
  511. # We try running the compiler with the flag for each vendor and
  512. # matching its regular expression in the output.
  513. function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang)
  514. if(NOT CMAKE_${lang}_COMPILER_ID_DIR)
  515. # We get here when this function is called not from within CMAKE_DETERMINE_COMPILER_ID()
  516. # This is done e.g. for detecting the compiler ID for assemblers.
  517. # Compute the directory in which to run the test and Create a clean working directory.
  518. set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_PLATFORM_INFO_DIR}/CompilerId${lang})
  519. file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  520. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  521. endif()
  522. foreach(vendor ${CMAKE_${lang}_COMPILER_ID_VENDORS})
  523. set(flags ${CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor}})
  524. set(regex ${CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor}})
  525. execute_process(
  526. COMMAND "${CMAKE_${lang}_COMPILER}"
  527. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  528. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  529. ${flags}
  530. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  531. OUTPUT_VARIABLE output ERROR_VARIABLE output
  532. RESULT_VARIABLE result
  533. TIMEOUT 10
  534. )
  535. if("${output}" MATCHES "${regex}")
  536. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  537. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  538. "matched \"${regex}\":\n${output}")
  539. set(CMAKE_${lang}_COMPILER_ID "${vendor}" PARENT_SCOPE)
  540. break()
  541. else()
  542. if("${result}" MATCHES "timeout")
  543. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  544. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  545. "terminated after 10 s due to timeout.")
  546. else()
  547. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  548. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  549. "did not match \"${regex}\":\n${output}")
  550. endif()
  551. endif()
  552. endforeach()
  553. endfunction()