CMakeDetermineCompilerId.cmake 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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(lang STREQUAL Fortran)
  127. set(v Intel)
  128. set(ext vfproj)
  129. set(id_cl ifort.exe)
  130. elseif(NOT "${vs_version}" VERSION_LESS 10)
  131. set(v 10)
  132. set(ext vcxproj)
  133. elseif(NOT "${vs_version}" VERSION_LESS 7)
  134. set(id_version ${vs_version}.00)
  135. set(v 7)
  136. set(ext vcproj)
  137. else()
  138. set(v 6)
  139. set(ext dsp)
  140. endif()
  141. if("${id_platform}" STREQUAL "Itanium")
  142. set(id_platform ia64)
  143. endif()
  144. if(CMAKE_VS_PLATFORM_TOOLSET)
  145. set(id_toolset "<PlatformToolset>${CMAKE_VS_PLATFORM_TOOLSET}</PlatformToolset>")
  146. if(CMAKE_VS_PLATFORM_TOOLSET MATCHES "Intel")
  147. set(id_cl icl.exe)
  148. endif()
  149. else()
  150. set(id_toolset "")
  151. endif()
  152. if(CMAKE_SYSTEM_NAME STREQUAL "WindowsPhone")
  153. set(id_system "<ApplicationType>Windows Phone</ApplicationType>")
  154. elseif(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
  155. set(id_system "<ApplicationType>Windows Store</ApplicationType>")
  156. else()
  157. set(id_system "")
  158. endif()
  159. if(id_system AND CMAKE_SYSTEM_VERSION)
  160. set(id_system_version "<ApplicationTypeRevision>${CMAKE_SYSTEM_VERSION}</ApplicationTypeRevision>")
  161. else()
  162. set(id_system_version "")
  163. endif()
  164. if(id_platform STREQUAL ARM)
  165. set(id_WindowsSDKDesktopARMSupport "<WindowsSDKDesktopARMSupport>true</WindowsSDKDesktopARMSupport>")
  166. else()
  167. set(id_WindowsSDKDesktopARMSupport "")
  168. endif()
  169. if(CMAKE_VS_WINCE_VERSION)
  170. set(id_entrypoint "mainACRTStartup")
  171. if("${vs_version}" VERSION_LESS 9)
  172. set(id_subsystem 9)
  173. else()
  174. set(id_subsystem 8)
  175. endif()
  176. else()
  177. set(id_subsystem 1)
  178. endif()
  179. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  180. get_filename_component(id_src "${src}" NAME)
  181. configure_file(${CMAKE_ROOT}/Modules/CompilerId/VS-${v}.${ext}.in
  182. ${id_dir}/CompilerId${lang}.${ext} @ONLY)
  183. if(CMAKE_VS_MSBUILD_COMMAND AND NOT lang STREQUAL "Fortran")
  184. set(command "${CMAKE_VS_MSBUILD_COMMAND}" "CompilerId${lang}.${ext}"
  185. "/p:Configuration=Debug" "/p:Platform=${id_platform}" "/p:VisualStudioVersion=${vs_version}.0"
  186. )
  187. elseif(CMAKE_VS_DEVENV_COMMAND)
  188. set(command "${CMAKE_VS_DEVENV_COMMAND}" "CompilerId${lang}.${ext}" "/build" "Debug")
  189. elseif(CMAKE_VS_MSDEV_COMMAND)
  190. set(command "${CMAKE_VS_MSDEV_COMMAND}" "CompilerId${lang}.${ext}" "/make")
  191. else()
  192. set(command "")
  193. endif()
  194. if(command)
  195. execute_process(
  196. COMMAND ${command}
  197. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  198. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  199. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  200. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  201. )
  202. else()
  203. set(CMAKE_${lang}_COMPILER_ID_RESULT 1)
  204. set(CMAKE_${lang}_COMPILER_ID_OUTPUT "VS environment not known to support ${lang}")
  205. endif()
  206. # Match the compiler location line printed out.
  207. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "CMAKE_${lang}_COMPILER=([^%\r\n]+)[\r\n]")
  208. # Strip VS diagnostic output from the end of the line.
  209. string(REGEX REPLACE " \\(TaskId:[0-9]*\\)$" "" _comp "${CMAKE_MATCH_1}")
  210. if(EXISTS "${_comp}")
  211. file(TO_CMAKE_PATH "${_comp}" _comp)
  212. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  213. endif()
  214. endif()
  215. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  216. set(id_lang "${lang}")
  217. set(id_type ${CMAKE_${lang}_COMPILER_XCODE_TYPE})
  218. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  219. get_filename_component(id_src "${src}" NAME)
  220. if(CMAKE_XCODE_PLATFORM_TOOLSET)
  221. set(id_toolset "GCC_VERSION = ${CMAKE_XCODE_PLATFORM_TOOLSET};")
  222. else()
  223. set(id_toolset "")
  224. endif()
  225. if(CMAKE_OSX_DEPLOYMENT_TARGET)
  226. set(id_deployment_target
  227. "MACOSX_DEPLOYMENT_TARGET = \"${CMAKE_OSX_DEPLOYMENT_TARGET}\";")
  228. else()
  229. set(id_deployment_target "")
  230. endif()
  231. if(CMAKE_OSX_SYSROOT)
  232. set(id_sdkroot "SDKROOT = \"${CMAKE_OSX_SYSROOT}\";")
  233. else()
  234. set(id_sdkroot "")
  235. endif()
  236. if(NOT ${XCODE_VERSION} VERSION_LESS 3)
  237. set(v 3)
  238. set(ext xcodeproj)
  239. elseif(NOT ${XCODE_VERSION} VERSION_LESS 2)
  240. set(v 2)
  241. set(ext xcodeproj)
  242. else()
  243. set(v 1)
  244. set(ext xcode)
  245. endif()
  246. configure_file(${CMAKE_ROOT}/Modules/CompilerId/Xcode-${v}.pbxproj.in
  247. ${id_dir}/CompilerId${lang}.${ext}/project.pbxproj @ONLY)
  248. unset(_ENV_MACOSX_DEPLOYMENT_TARGET)
  249. if(DEFINED ENV{MACOSX_DEPLOYMENT_TARGET})
  250. set(_ENV_MACOSX_DEPLOYMENT_TARGET "$ENV{MACOSX_DEPLOYMENT_TARGET}")
  251. set(ENV{MACOSX_DEPLOYMENT_TARGET} "")
  252. endif()
  253. execute_process(COMMAND xcodebuild
  254. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  255. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  256. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  257. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  258. )
  259. if(DEFINED _ENV_MACOSX_DEPLOYMENT_TARGET)
  260. set(ENV{MACOSX_DEPLOYMENT_TARGET} "${_ENV_MACOSX_DEPLOYMENT_TARGET}")
  261. endif()
  262. # Match the link line from xcodebuild output of the form
  263. # Ld ...
  264. # ...
  265. # /path/to/cc ...CompilerId${lang}/...
  266. # to extract the compiler front-end for the language.
  267. 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}[ \t\n\\\"]")
  268. set(_comp "${CMAKE_MATCH_2}")
  269. if(EXISTS "${_comp}")
  270. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  271. endif()
  272. endif()
  273. else()
  274. if(COMMAND EXECUTE_PROCESS)
  275. execute_process(
  276. COMMAND "${CMAKE_${lang}_COMPILER}"
  277. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  278. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  279. ${testflags}
  280. "${src}"
  281. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  282. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  283. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  284. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  285. )
  286. else()
  287. exec_program(
  288. "${CMAKE_${lang}_COMPILER}" ${CMAKE_${lang}_COMPILER_ID_DIR}
  289. ARGS ${CMAKE_${lang}_COMPILER_ID_ARG1}
  290. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  291. ${testflags}
  292. \"${src}\"
  293. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  294. RETURN_VALUE CMAKE_${lang}_COMPILER_ID_RESULT
  295. )
  296. endif()
  297. endif()
  298. # Check the result of compilation.
  299. if(CMAKE_${lang}_COMPILER_ID_RESULT
  300. # Intel Fortran warns and ignores preprocessor lines without /fpp
  301. OR CMAKE_${lang}_COMPILER_ID_OUTPUT MATCHES "Bad # preprocessor line"
  302. )
  303. # Compilation failed.
  304. set(MSG
  305. "Compiling the ${lang} compiler identification source file \"${src}\" failed.
  306. ${COMPILER_DESCRIPTION}
  307. The output was:
  308. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  309. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  310. ")
  311. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log "${MSG}")
  312. #if(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  313. # message(FATAL_ERROR "${MSG}")
  314. #endif()
  315. # No output files should be inspected.
  316. set(COMPILER_${lang}_PRODUCED_FILES)
  317. else()
  318. # Compilation succeeded.
  319. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  320. "Compiling the ${lang} compiler identification source file \"${src}\" succeeded.
  321. ${COMPILER_DESCRIPTION}
  322. The output was:
  323. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  324. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  325. ")
  326. # Find the executable produced by the compiler, try all files in the
  327. # binary dir.
  328. file(GLOB files
  329. RELATIVE ${CMAKE_${lang}_COMPILER_ID_DIR}
  330. ${CMAKE_${lang}_COMPILER_ID_DIR}/*)
  331. list(REMOVE_ITEM files "${src}")
  332. set(COMPILER_${lang}_PRODUCED_FILES "")
  333. foreach(file ${files})
  334. if(NOT IS_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}/${file})
  335. list(APPEND COMPILER_${lang}_PRODUCED_FILES ${file})
  336. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  337. "Compilation of the ${lang} compiler identification source \""
  338. "${src}\" produced \"${file}\"\n\n")
  339. endif()
  340. endforeach()
  341. if(NOT COMPILER_${lang}_PRODUCED_FILES)
  342. # No executable was found.
  343. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  344. "Compilation of the ${lang} compiler identification source \""
  345. "${src}\" did not produce an executable in \""
  346. "${CMAKE_${lang}_COMPILER_ID_DIR}\".\n\n")
  347. endif()
  348. endif()
  349. # Return the files produced by the compilation.
  350. set(COMPILER_${lang}_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE)
  351. endfunction()
  352. #-----------------------------------------------------------------------------
  353. # Function to extract the compiler id from an executable.
  354. function(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file)
  355. # Look for a compiler id if not yet known.
  356. if(NOT CMAKE_${lang}_COMPILER_ID)
  357. # Read the compiler identification string from the executable file.
  358. set(COMPILER_ID)
  359. set(COMPILER_VERSION)
  360. set(PLATFORM_ID)
  361. set(ARCHITECTURE_ID)
  362. set(SIMULATE_ID)
  363. set(SIMULATE_VERSION)
  364. file(STRINGS ${file}
  365. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 6 REGEX "INFO:[A-Za-z0-9_]+\\[[^]]*\\]")
  366. set(COMPILER_ID_TWICE)
  367. foreach(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  368. if("${info}" MATCHES "INFO:compiler\\[([^]\"]*)\\]")
  369. if(COMPILER_ID)
  370. set(COMPILER_ID_TWICE 1)
  371. endif()
  372. set(COMPILER_ID "${CMAKE_MATCH_1}")
  373. endif()
  374. if("${info}" MATCHES "INFO:platform\\[([^]\"]*)\\]")
  375. set(PLATFORM_ID "${CMAKE_MATCH_1}")
  376. endif()
  377. if("${info}" MATCHES "INFO:arch\\[([^]\"]*)\\]")
  378. set(ARCHITECTURE_ID "${CMAKE_MATCH_1}")
  379. endif()
  380. if("${info}" MATCHES "INFO:compiler_version\\[([^]\"]*)\\]")
  381. string(REGEX REPLACE "^0+([0-9])" "\\1" COMPILER_VERSION "${CMAKE_MATCH_1}")
  382. string(REGEX REPLACE "\\.0+([0-9])" ".\\1" COMPILER_VERSION "${COMPILER_VERSION}")
  383. endif()
  384. if("${info}" MATCHES "INFO:simulate\\[([^]\"]*)\\]")
  385. set(SIMULATE_ID "${CMAKE_MATCH_1}")
  386. endif()
  387. if("${info}" MATCHES "INFO:simulate_version\\[([^]\"]*)\\]")
  388. string(REGEX REPLACE "^0+([0-9])" "\\1" SIMULATE_VERSION "${CMAKE_MATCH_1}")
  389. string(REGEX REPLACE "\\.0+([0-9])" ".\\1" SIMULATE_VERSION "${SIMULATE_VERSION}")
  390. endif()
  391. if("${info}" MATCHES "INFO:qnxnto")
  392. set(COMPILER_QNXNTO 1)
  393. endif()
  394. endforeach()
  395. # Detect the exact architecture from the PE header.
  396. if(WIN32)
  397. # The offset to the PE signature is stored at 0x3c.
  398. file(READ ${file} peoffsethex LIMIT 1 OFFSET 60 HEX)
  399. string(SUBSTRING "${peoffsethex}" 0 1 peoffsethex1)
  400. string(SUBSTRING "${peoffsethex}" 1 1 peoffsethex2)
  401. set(peoffsetexpression "${peoffsethex1} * 16 + ${peoffsethex2}")
  402. string(REPLACE "a" "10" peoffsetexpression "${peoffsetexpression}")
  403. string(REPLACE "b" "11" peoffsetexpression "${peoffsetexpression}")
  404. string(REPLACE "c" "12" peoffsetexpression "${peoffsetexpression}")
  405. string(REPLACE "d" "13" peoffsetexpression "${peoffsetexpression}")
  406. string(REPLACE "e" "14" peoffsetexpression "${peoffsetexpression}")
  407. string(REPLACE "f" "15" peoffsetexpression "${peoffsetexpression}")
  408. math(EXPR peoffset "${peoffsetexpression}")
  409. file(READ ${file} peheader LIMIT 6 OFFSET ${peoffset} HEX)
  410. if(peheader STREQUAL "50450000a201")
  411. set(ARCHITECTURE_ID "SH3")
  412. elseif(peheader STREQUAL "50450000a301")
  413. set(ARCHITECTURE_ID "SH3DSP")
  414. elseif(peheader STREQUAL "50450000a601")
  415. set(ARCHITECTURE_ID "SH4")
  416. elseif(peheader STREQUAL "50450000a801")
  417. set(ARCHITECTURE_ID "SH5")
  418. elseif(peheader STREQUAL "50450000c201")
  419. set(ARCHITECTURE_ID "THUMB")
  420. endif()
  421. endif()
  422. # Check if a valid compiler and platform were found.
  423. if(COMPILER_ID AND NOT COMPILER_ID_TWICE)
  424. set(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
  425. set(CMAKE_${lang}_PLATFORM_ID "${PLATFORM_ID}")
  426. set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
  427. set(CMAKE_${lang}_COMPILER_VERSION "${COMPILER_VERSION}")
  428. set(CMAKE_${lang}_SIMULATE_ID "${SIMULATE_ID}")
  429. set(CMAKE_${lang}_SIMULATE_VERSION "${SIMULATE_VERSION}")
  430. endif()
  431. # Check the compiler identification string.
  432. if(CMAKE_${lang}_COMPILER_ID)
  433. # The compiler identification was found.
  434. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  435. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
  436. "${file}\"\n\n")
  437. else()
  438. # The compiler identification could not be found.
  439. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  440. "The ${lang} compiler identification could not be found in \""
  441. "${file}\"\n\n")
  442. endif()
  443. endif()
  444. # try to figure out the executable format: ELF, COFF, Mach-O
  445. if(NOT CMAKE_EXECUTABLE_FORMAT)
  446. file(READ ${file} CMAKE_EXECUTABLE_MAGIC LIMIT 4 HEX)
  447. # ELF files start with 0x7f"ELF"
  448. if("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
  449. set(CMAKE_EXECUTABLE_FORMAT "ELF" CACHE INTERNAL "Executable file format")
  450. endif()
  451. # # COFF (.exe) files start with "MZ"
  452. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
  453. # set(CMAKE_EXECUTABLE_FORMAT "COFF" CACHE STRING "Executable file format")
  454. # endif()
  455. #
  456. # # Mach-O files start with CAFEBABE or FEEDFACE, according to http://radio.weblogs.com/0100490/2003/01/28.html
  457. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe")
  458. # set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  459. # endif()
  460. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface")
  461. # set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  462. # endif()
  463. endif()
  464. if(NOT DEFINED CMAKE_EXECUTABLE_FORMAT)
  465. set(CMAKE_EXECUTABLE_FORMAT)
  466. endif()
  467. # Return the information extracted.
  468. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  469. set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  470. set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  471. PARENT_SCOPE)
  472. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
  473. set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE)
  474. set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE)
  475. set(CMAKE_EXECUTABLE_FORMAT "${CMAKE_EXECUTABLE_FORMAT}" PARENT_SCOPE)
  476. set(COMPILER_QNXNTO "${COMPILER_QNXNTO}" PARENT_SCOPE)
  477. endfunction()
  478. #-----------------------------------------------------------------------------
  479. # Function to query the compiler vendor.
  480. # This uses a table with entries of the form
  481. # list(APPEND CMAKE_${lang}_COMPILER_ID_VENDORS ${vendor})
  482. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor} -some-vendor-flag)
  483. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor} "Some Vendor Output")
  484. # We try running the compiler with the flag for each vendor and
  485. # matching its regular expression in the output.
  486. function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang)
  487. if(NOT CMAKE_${lang}_COMPILER_ID_DIR)
  488. # We get here when this function is called not from within CMAKE_DETERMINE_COMPILER_ID()
  489. # This is done e.g. for detecting the compiler ID for assemblers.
  490. # Compute the directory in which to run the test and Create a clean working directory.
  491. set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_PLATFORM_INFO_DIR}/CompilerId${lang})
  492. file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  493. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  494. endif()
  495. foreach(vendor ${CMAKE_${lang}_COMPILER_ID_VENDORS})
  496. set(flags ${CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor}})
  497. set(regex ${CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor}})
  498. execute_process(
  499. COMMAND "${CMAKE_${lang}_COMPILER}"
  500. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  501. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  502. ${flags}
  503. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  504. OUTPUT_VARIABLE output ERROR_VARIABLE output
  505. RESULT_VARIABLE result
  506. TIMEOUT 10
  507. )
  508. if("${output}" MATCHES "${regex}")
  509. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  510. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  511. "matched \"${regex}\":\n${output}")
  512. set(CMAKE_${lang}_COMPILER_ID "${vendor}" PARENT_SCOPE)
  513. break()
  514. else()
  515. if("${result}" MATCHES "timeout")
  516. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  517. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  518. "terminated after 10 s due to timeout.")
  519. else()
  520. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  521. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  522. "did not match \"${regex}\":\n${output}")
  523. endif()
  524. endif()
  525. endforeach()
  526. endfunction()