CMakeDetermineCompilerId.cmake 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # Function to compile a source file to identify the compiler. This is
  4. # used internally by CMake and should not be included by user code.
  5. # If successful, sets CMAKE_<lang>_COMPILER_ID and CMAKE_<lang>_PLATFORM_ID
  6. function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
  7. # Make sure the compiler arguments are clean.
  8. string(STRIP "${CMAKE_${lang}_COMPILER_ARG1}" CMAKE_${lang}_COMPILER_ID_ARG1)
  9. string(REGEX REPLACE " +" ";" CMAKE_${lang}_COMPILER_ID_ARG1 "${CMAKE_${lang}_COMPILER_ID_ARG1}")
  10. # Make sure user-specified compiler flags are used.
  11. if(CMAKE_${lang}_FLAGS)
  12. set(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS})
  13. else()
  14. set(CMAKE_${lang}_COMPILER_ID_FLAGS $ENV{${flagvar}})
  15. endif()
  16. string(REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}")
  17. # Compute the directory in which to run the test.
  18. set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_PLATFORM_INFO_DIR}/CompilerId${lang})
  19. # Try building with no extra flags and then try each set
  20. # of helper flags. Stop when the compiler is identified.
  21. foreach(flags ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS_FIRST}
  22. ""
  23. ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS})
  24. CMAKE_DETERMINE_COMPILER_ID_BUILD("${lang}" "${flags}" "${src}")
  25. CMAKE_DETERMINE_COMPILER_ID_MATCH_VENDOR("${lang}" "${COMPILER_${lang}_PRODUCED_OUTPUT}")
  26. if(CMAKE_${lang}_COMPILER_ID)
  27. break()
  28. endif()
  29. foreach(file ${COMPILER_${lang}_PRODUCED_FILES})
  30. CMAKE_DETERMINE_COMPILER_ID_CHECK("${lang}" "${CMAKE_${lang}_COMPILER_ID_DIR}/${file}" "${src}")
  31. endforeach()
  32. if(CMAKE_${lang}_COMPILER_ID)
  33. break()
  34. endif()
  35. endforeach()
  36. # If the compiler is still unknown, try to query its vendor.
  37. if(CMAKE_${lang}_COMPILER AND NOT CMAKE_${lang}_COMPILER_ID)
  38. CMAKE_DETERMINE_COMPILER_ID_VENDOR(${lang})
  39. endif()
  40. if (COMPILER_QNXNTO AND CMAKE_${lang}_COMPILER_ID STREQUAL "GNU")
  41. execute_process(
  42. COMMAND "${CMAKE_${lang}_COMPILER}"
  43. -V
  44. OUTPUT_VARIABLE output ERROR_VARIABLE output
  45. RESULT_VARIABLE result
  46. TIMEOUT 10
  47. )
  48. if (output MATCHES "targets available")
  49. set(CMAKE_${lang}_COMPILER_ID QCC)
  50. # http://community.qnx.com/sf/discussion/do/listPosts/projects.community/discussion.qnx_momentics_community_support.topc3555?_pagenum=2
  51. # The qcc driver does not itself have a version.
  52. endif()
  53. endif()
  54. # if the format is unknown after all files have been checked, put "Unknown" in the cache
  55. if(NOT CMAKE_EXECUTABLE_FORMAT)
  56. set(CMAKE_EXECUTABLE_FORMAT "Unknown" CACHE INTERNAL "Executable file format")
  57. endif()
  58. if(CMAKE_GENERATOR STREQUAL "Ninja" AND MSVC_${lang}_ARCHITECTURE_ID)
  59. CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX(${lang})
  60. else()
  61. set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "")
  62. endif()
  63. # Display the final identification result.
  64. if(CMAKE_${lang}_COMPILER_ID)
  65. if(CMAKE_${lang}_COMPILER_VERSION)
  66. set(_version " ${CMAKE_${lang}_COMPILER_VERSION}")
  67. else()
  68. set(_version "")
  69. endif()
  70. message(STATUS "The ${lang} compiler identification is "
  71. "${CMAKE_${lang}_COMPILER_ID}${_version}")
  72. else()
  73. message(STATUS "The ${lang} compiler identification is unknown")
  74. endif()
  75. # Check if compiler id detection gave us the compiler tool.
  76. if(CMAKE_${lang}_COMPILER_ID_TOOL)
  77. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER_ID_TOOL}" PARENT_SCOPE)
  78. elseif(NOT CMAKE_${lang}_COMPILER)
  79. set(CMAKE_${lang}_COMPILER "CMAKE_${lang}_COMPILER-NOTFOUND" PARENT_SCOPE)
  80. endif()
  81. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  82. set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  83. set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  84. PARENT_SCOPE)
  85. set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "${CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX}" PARENT_SCOPE)
  86. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
  87. set(CMAKE_${lang}_COMPILER_WRAPPER "${CMAKE_${lang}_COMPILER_WRAPPER}" PARENT_SCOPE)
  88. set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE)
  89. set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE)
  90. set(CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT "${CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT}" PARENT_SCOPE)
  91. endfunction()
  92. include(CMakeCompilerIdDetection)
  93. #-----------------------------------------------------------------------------
  94. # Function to write the compiler id source file.
  95. function(CMAKE_DETERMINE_COMPILER_ID_WRITE lang src)
  96. find_file(src_in ${src}.in PATHS ${CMAKE_ROOT}/Modules ${CMAKE_MODULE_PATH} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
  97. file(READ ${src_in} ID_CONTENT_IN)
  98. compiler_id_detection(CMAKE_${lang}_COMPILER_ID_CONTENT ${lang}
  99. ID_STRING
  100. VERSION_STRINGS
  101. PLATFORM_DEFAULT_COMPILER
  102. )
  103. unset(src_in CACHE)
  104. string(CONFIGURE "${ID_CONTENT_IN}" ID_CONTENT_OUT @ONLY)
  105. file(WRITE ${CMAKE_${lang}_COMPILER_ID_DIR}/${src} "${ID_CONTENT_OUT}")
  106. endfunction()
  107. #-----------------------------------------------------------------------------
  108. # Function to build the compiler id source file and look for output
  109. # files.
  110. function(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags src)
  111. # Create a clean working directory.
  112. file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  113. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  114. CMAKE_DETERMINE_COMPILER_ID_WRITE("${lang}" "${src}")
  115. # Construct a description of this test case.
  116. set(COMPILER_DESCRIPTION
  117. "Compiler: ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1}
  118. Build flags: ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  119. Id flags: ${testflags}
  120. ")
  121. # Compile the compiler identification source.
  122. if("${CMAKE_GENERATOR}" MATCHES "Visual Studio ([0-9]+)")
  123. set(vs_version ${CMAKE_MATCH_1})
  124. set(id_platform ${CMAKE_VS_PLATFORM_NAME})
  125. set(id_lang "${lang}")
  126. if(CMAKE_VS_PLATFORM_TOOLSET MATCHES "v[0-9]+_clang_.*")
  127. set(id_cl clang.exe)
  128. else()
  129. set(id_cl cl.exe)
  130. endif()
  131. if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
  132. set(v NsightTegra)
  133. set(ext vcxproj)
  134. if(lang STREQUAL CXX)
  135. set(id_gcc g++)
  136. set(id_clang clang++)
  137. else()
  138. set(id_gcc gcc)
  139. set(id_clang clang)
  140. endif()
  141. elseif(lang STREQUAL Fortran)
  142. set(v Intel)
  143. set(ext vfproj)
  144. set(id_cl ifort.exe)
  145. elseif(NOT "${vs_version}" VERSION_LESS 10)
  146. set(v 10)
  147. set(ext vcxproj)
  148. else()
  149. set(id_version ${vs_version}.00)
  150. set(v 7)
  151. set(ext vcproj)
  152. endif()
  153. if(CMAKE_VS_PLATFORM_TOOLSET)
  154. if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
  155. set(id_toolset "<NdkToolchainVersion>${CMAKE_VS_PLATFORM_TOOLSET}</NdkToolchainVersion>")
  156. else()
  157. set(id_toolset "<PlatformToolset>${CMAKE_VS_PLATFORM_TOOLSET}</PlatformToolset>")
  158. if(CMAKE_VS_PLATFORM_TOOLSET MATCHES "Intel")
  159. set(id_cl icl.exe)
  160. endif()
  161. endif()
  162. else()
  163. set(id_toolset "")
  164. endif()
  165. if(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE)
  166. set(id_PreferredToolArchitecture "<PreferredToolArchitecture>${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}</PreferredToolArchitecture>")
  167. else()
  168. set(id_PreferredToolArchitecture "")
  169. endif()
  170. if(CMAKE_SYSTEM_NAME STREQUAL "WindowsPhone")
  171. set(id_system "<ApplicationType>Windows Phone</ApplicationType>")
  172. elseif(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
  173. set(id_system "<ApplicationType>Windows Store</ApplicationType>")
  174. else()
  175. set(id_system "")
  176. endif()
  177. if(id_system AND CMAKE_SYSTEM_VERSION)
  178. set(id_system_version "<ApplicationTypeRevision>${CMAKE_SYSTEM_VERSION}</ApplicationTypeRevision>")
  179. else()
  180. set(id_system_version "")
  181. endif()
  182. if(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
  183. set(id_WindowsTargetPlatformVersion "<WindowsTargetPlatformVersion>${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}</WindowsTargetPlatformVersion>")
  184. endif()
  185. if(id_platform STREQUAL ARM)
  186. set(id_WindowsSDKDesktopARMSupport "<WindowsSDKDesktopARMSupport>true</WindowsSDKDesktopARMSupport>")
  187. else()
  188. set(id_WindowsSDKDesktopARMSupport "")
  189. endif()
  190. if(CMAKE_VS_WINCE_VERSION)
  191. set(id_entrypoint "mainACRTStartup")
  192. if("${vs_version}" VERSION_LESS 9)
  193. set(id_subsystem 9)
  194. else()
  195. set(id_subsystem 8)
  196. endif()
  197. else()
  198. set(id_subsystem 1)
  199. endif()
  200. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  201. set(id_src "${src}")
  202. configure_file(${CMAKE_ROOT}/Modules/CompilerId/VS-${v}.${ext}.in
  203. ${id_dir}/CompilerId${lang}.${ext} @ONLY)
  204. if(CMAKE_VS_MSBUILD_COMMAND AND NOT lang STREQUAL "Fortran")
  205. set(command "${CMAKE_VS_MSBUILD_COMMAND}" "CompilerId${lang}.${ext}"
  206. "/p:Configuration=Debug" "/p:Platform=${id_platform}" "/p:VisualStudioVersion=${vs_version}.0"
  207. )
  208. elseif(CMAKE_VS_DEVENV_COMMAND)
  209. set(command "${CMAKE_VS_DEVENV_COMMAND}" "CompilerId${lang}.${ext}" "/build" "Debug")
  210. else()
  211. set(command "")
  212. endif()
  213. if(command)
  214. execute_process(
  215. COMMAND ${command}
  216. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  217. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  218. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  219. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  220. )
  221. else()
  222. set(CMAKE_${lang}_COMPILER_ID_RESULT 1)
  223. set(CMAKE_${lang}_COMPILER_ID_OUTPUT "VS environment not known to support ${lang}")
  224. endif()
  225. # Match the compiler location line printed out.
  226. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "CMAKE_${lang}_COMPILER=([^%\r\n]+)[\r\n]")
  227. # Strip VS diagnostic output from the end of the line.
  228. string(REGEX REPLACE " \\(TaskId:[0-9]*\\)$" "" _comp "${CMAKE_MATCH_1}")
  229. if(EXISTS "${_comp}")
  230. file(TO_CMAKE_PATH "${_comp}" _comp)
  231. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  232. endif()
  233. endif()
  234. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  235. set(id_lang "${lang}")
  236. set(id_type ${CMAKE_${lang}_COMPILER_XCODE_TYPE})
  237. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  238. set(id_src "${src}")
  239. if(CMAKE_XCODE_PLATFORM_TOOLSET)
  240. set(id_toolset "GCC_VERSION = ${CMAKE_XCODE_PLATFORM_TOOLSET};")
  241. else()
  242. set(id_toolset "")
  243. endif()
  244. if("${lang}" STREQUAL "Swift")
  245. set(id_lang_version "SWIFT_VERSION = 2.3;")
  246. else()
  247. set(id_lang_version "")
  248. endif()
  249. if(CMAKE_OSX_DEPLOYMENT_TARGET)
  250. set(id_deployment_target
  251. "MACOSX_DEPLOYMENT_TARGET = \"${CMAKE_OSX_DEPLOYMENT_TARGET}\";")
  252. else()
  253. set(id_deployment_target "")
  254. endif()
  255. set(id_product_type "com.apple.product-type.tool")
  256. if(CMAKE_OSX_SYSROOT)
  257. set(id_sdkroot "SDKROOT = \"${CMAKE_OSX_SYSROOT}\";")
  258. if(CMAKE_OSX_SYSROOT MATCHES "(^|/)[Ii][Pp][Hh][Oo][Nn][Ee]")
  259. set(id_product_type "com.apple.product-type.bundle.unit-test")
  260. endif()
  261. else()
  262. set(id_sdkroot "")
  263. endif()
  264. if(NOT ${XCODE_VERSION} VERSION_LESS 3)
  265. set(v 3)
  266. set(ext xcodeproj)
  267. elseif(NOT ${XCODE_VERSION} VERSION_LESS 2)
  268. set(v 2)
  269. set(ext xcodeproj)
  270. else()
  271. set(v 1)
  272. set(ext xcode)
  273. endif()
  274. configure_file(${CMAKE_ROOT}/Modules/CompilerId/Xcode-${v}.pbxproj.in
  275. ${id_dir}/CompilerId${lang}.${ext}/project.pbxproj @ONLY)
  276. unset(_ENV_MACOSX_DEPLOYMENT_TARGET)
  277. if(DEFINED ENV{MACOSX_DEPLOYMENT_TARGET})
  278. set(_ENV_MACOSX_DEPLOYMENT_TARGET "$ENV{MACOSX_DEPLOYMENT_TARGET}")
  279. set(ENV{MACOSX_DEPLOYMENT_TARGET} "")
  280. endif()
  281. execute_process(COMMAND xcodebuild
  282. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  283. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  284. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  285. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  286. )
  287. if(DEFINED _ENV_MACOSX_DEPLOYMENT_TARGET)
  288. set(ENV{MACOSX_DEPLOYMENT_TARGET} "${_ENV_MACOSX_DEPLOYMENT_TARGET}")
  289. endif()
  290. if(DEFINED CMAKE_${lang}_COMPILER_ID_TOOL_MATCH_REGEX)
  291. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "${CMAKE_${lang}_COMPILER_ID_TOOL_MATCH_REGEX}")
  292. set(_comp "${CMAKE_MATCH_${CMAKE_${lang}_COMPILER_ID_TOOL_MATCH_INDEX}}")
  293. if(EXISTS "${_comp}")
  294. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  295. endif()
  296. endif()
  297. endif()
  298. else()
  299. execute_process(
  300. COMMAND "${CMAKE_${lang}_COMPILER}"
  301. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  302. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  303. ${testflags}
  304. "${src}"
  305. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  306. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  307. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  308. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  309. )
  310. endif()
  311. # Check the result of compilation.
  312. if(CMAKE_${lang}_COMPILER_ID_RESULT
  313. # Intel Fortran warns and ignores preprocessor lines without /fpp
  314. OR CMAKE_${lang}_COMPILER_ID_OUTPUT MATCHES "Bad # preprocessor line"
  315. )
  316. # Compilation failed.
  317. set(MSG
  318. "Compiling the ${lang} compiler identification source file \"${src}\" failed.
  319. ${COMPILER_DESCRIPTION}
  320. The output was:
  321. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  322. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  323. ")
  324. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log "${MSG}")
  325. #if(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  326. # message(FATAL_ERROR "${MSG}")
  327. #endif()
  328. # No output files should be inspected.
  329. set(COMPILER_${lang}_PRODUCED_FILES)
  330. set(COMPILER_${lang}_PRODUCED_OUTPUT)
  331. else()
  332. # Compilation succeeded.
  333. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  334. "Compiling the ${lang} compiler identification source file \"${src}\" succeeded.
  335. ${COMPILER_DESCRIPTION}
  336. The output was:
  337. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  338. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  339. ")
  340. # Find the executable produced by the compiler, try all files in the
  341. # binary dir.
  342. string(REGEX REPLACE "([][])" "[\\1]" _glob_id_dir "${CMAKE_${lang}_COMPILER_ID_DIR}")
  343. file(GLOB files
  344. RELATIVE ${CMAKE_${lang}_COMPILER_ID_DIR}
  345. # normal case
  346. ${_glob_id_dir}/*
  347. # com.apple.package-type.bundle.unit-test
  348. ${_glob_id_dir}/*.xctest/*
  349. )
  350. list(REMOVE_ITEM files "${src}")
  351. set(COMPILER_${lang}_PRODUCED_FILES "")
  352. foreach(file ${files})
  353. if(NOT IS_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}/${file})
  354. list(APPEND COMPILER_${lang}_PRODUCED_FILES ${file})
  355. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  356. "Compilation of the ${lang} compiler identification source \""
  357. "${src}\" produced \"${file}\"\n\n")
  358. endif()
  359. endforeach()
  360. if(NOT COMPILER_${lang}_PRODUCED_FILES)
  361. # No executable was found.
  362. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  363. "Compilation of the ${lang} compiler identification source \""
  364. "${src}\" did not produce an executable in \""
  365. "${CMAKE_${lang}_COMPILER_ID_DIR}\".\n\n")
  366. endif()
  367. set(COMPILER_${lang}_PRODUCED_OUTPUT "${CMAKE_${lang}_COMPILER_ID_OUTPUT}")
  368. endif()
  369. # Return the files produced by the compilation.
  370. set(COMPILER_${lang}_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE)
  371. set(COMPILER_${lang}_PRODUCED_OUTPUT "${COMPILER_${lang}_PRODUCED_OUTPUT}" PARENT_SCOPE)
  372. endfunction()
  373. #-----------------------------------------------------------------------------
  374. # Function to extract the compiler id from compiler output.
  375. function(CMAKE_DETERMINE_COMPILER_ID_MATCH_VENDOR lang output)
  376. foreach(vendor ${CMAKE_${lang}_COMPILER_ID_MATCH_VENDORS})
  377. if(output MATCHES "${CMAKE_${lang}_COMPILER_ID_MATCH_VENDOR_REGEX_${vendor}}")
  378. set(CMAKE_${lang}_COMPILER_ID "${vendor}")
  379. endif()
  380. endforeach()
  381. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  382. endfunction()
  383. #-----------------------------------------------------------------------------
  384. # Function to extract the compiler id from an executable.
  385. function(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file)
  386. # Look for a compiler id if not yet known.
  387. if(NOT CMAKE_${lang}_COMPILER_ID)
  388. # Read the compiler identification string from the executable file.
  389. set(COMPILER_ID)
  390. set(COMPILER_VERSION)
  391. set(COMPILER_VERSION_MAJOR 0)
  392. set(COMPILER_VERSION_MINOR 0)
  393. set(COMPILER_VERSION_PATCH 0)
  394. set(COMPILER_VERSION_TWEAK 0)
  395. set(HAVE_COMPILER_VERSION_MAJOR 0)
  396. set(HAVE_COMPILER_VERSION_MINOR 0)
  397. set(HAVE_COMPILER_VERSION_PATCH 0)
  398. set(HAVE_COMPILER_VERSION_TWEAK 0)
  399. set(COMPILER_WRAPPER)
  400. set(DIGIT_VALUE_1 1)
  401. set(DIGIT_VALUE_2 10)
  402. set(DIGIT_VALUE_3 100)
  403. set(DIGIT_VALUE_4 1000)
  404. set(DIGIT_VALUE_5 10000)
  405. set(DIGIT_VALUE_6 100000)
  406. set(DIGIT_VALUE_7 1000000)
  407. set(DIGIT_VALUE_8 10000000)
  408. set(PLATFORM_ID)
  409. set(ARCHITECTURE_ID)
  410. set(SIMULATE_ID)
  411. set(SIMULATE_VERSION)
  412. file(STRINGS ${file}
  413. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 38 REGEX "INFO:[A-Za-z0-9_]+\\[[^]]*\\]")
  414. set(COMPILER_ID_TWICE)
  415. foreach(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  416. if("${info}" MATCHES "INFO:compiler\\[([^]\"]*)\\]")
  417. if(COMPILER_ID)
  418. set(COMPILER_ID_TWICE 1)
  419. endif()
  420. set(COMPILER_ID "${CMAKE_MATCH_1}")
  421. endif()
  422. if("${info}" MATCHES "INFO:platform\\[([^]\"]*)\\]")
  423. set(PLATFORM_ID "${CMAKE_MATCH_1}")
  424. endif()
  425. if("${info}" MATCHES "INFO:arch\\[([^]\"]*)\\]")
  426. set(ARCHITECTURE_ID "${CMAKE_MATCH_1}")
  427. endif()
  428. if("${info}" MATCHES "INFO:compiler_version\\[([^]\"]*)\\]")
  429. string(REGEX REPLACE "^0+([0-9])" "\\1" COMPILER_VERSION "${CMAKE_MATCH_1}")
  430. string(REGEX REPLACE "\\.0+([0-9])" ".\\1" COMPILER_VERSION "${COMPILER_VERSION}")
  431. endif()
  432. foreach(comp MAJOR MINOR PATCH TWEAK)
  433. foreach(digit 1 2 3 4 5 6 7 8 9)
  434. if("${info}" MATCHES "INFO:compiler_version_${comp}_digit_${digit}\\[([0-9])\\]")
  435. set(value ${CMAKE_MATCH_1})
  436. math(EXPR COMPILER_VERSION_${comp} "${COMPILER_VERSION_${comp}} + ${value} * ${DIGIT_VALUE_${digit}}")
  437. set(HAVE_COMPILER_VERSION_${comp} 1)
  438. endif()
  439. endforeach()
  440. endforeach()
  441. if("${info}" MATCHES "INFO:compiler_wrapper\\[([^]\"]*)\\]")
  442. set(COMPILER_WRAPPER "${CMAKE_MATCH_1}")
  443. endif()
  444. if("${info}" MATCHES "INFO:simulate\\[([^]\"]*)\\]")
  445. set(SIMULATE_ID "${CMAKE_MATCH_1}")
  446. endif()
  447. if("${info}" MATCHES "INFO:simulate_version\\[([^]\"]*)\\]")
  448. string(REGEX REPLACE "^0+([0-9])" "\\1" SIMULATE_VERSION "${CMAKE_MATCH_1}")
  449. string(REGEX REPLACE "\\.0+([0-9])" ".\\1" SIMULATE_VERSION "${SIMULATE_VERSION}")
  450. endif()
  451. if("${info}" MATCHES "INFO:qnxnto\\[\\]")
  452. set(COMPILER_QNXNTO 1)
  453. endif()
  454. if("${info}" MATCHES "INFO:dialect_default\\[([^]\"]*)\\]")
  455. set(CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT "${CMAKE_MATCH_1}")
  456. endif()
  457. endforeach()
  458. # Construct compiler version from components if needed.
  459. if(NOT DEFINED COMPILER_VERSION AND HAVE_COMPILER_VERSION_MAJOR)
  460. set(COMPILER_VERSION "${COMPILER_VERSION_MAJOR}")
  461. if(HAVE_COMPILER_VERSION_MINOR)
  462. string(APPEND COMPILER_VERSION ".${COMPILER_VERSION_MINOR}")
  463. if(HAVE_COMPILER_VERSION_PATCH)
  464. string(APPEND COMPILER_VERSION ".${COMPILER_VERSION_PATCH}")
  465. if(HAVE_COMPILER_VERSION_TWEAK)
  466. string(APPEND COMPILER_VERSION ".${COMPILER_VERSION_TWEAK}")
  467. endif()
  468. endif()
  469. endif()
  470. endif()
  471. # Detect the exact architecture from the PE header.
  472. if(WIN32)
  473. # The offset to the PE signature is stored at 0x3c.
  474. file(READ ${file} peoffsethex LIMIT 1 OFFSET 60 HEX)
  475. string(SUBSTRING "${peoffsethex}" 0 1 peoffsethex1)
  476. string(SUBSTRING "${peoffsethex}" 1 1 peoffsethex2)
  477. set(peoffsetexpression "${peoffsethex1} * 16 + ${peoffsethex2}")
  478. string(REPLACE "a" "10" peoffsetexpression "${peoffsetexpression}")
  479. string(REPLACE "b" "11" peoffsetexpression "${peoffsetexpression}")
  480. string(REPLACE "c" "12" peoffsetexpression "${peoffsetexpression}")
  481. string(REPLACE "d" "13" peoffsetexpression "${peoffsetexpression}")
  482. string(REPLACE "e" "14" peoffsetexpression "${peoffsetexpression}")
  483. string(REPLACE "f" "15" peoffsetexpression "${peoffsetexpression}")
  484. math(EXPR peoffset "${peoffsetexpression}")
  485. file(READ ${file} peheader LIMIT 6 OFFSET ${peoffset} HEX)
  486. if(peheader STREQUAL "50450000a201")
  487. set(ARCHITECTURE_ID "SH3")
  488. elseif(peheader STREQUAL "50450000a301")
  489. set(ARCHITECTURE_ID "SH3DSP")
  490. elseif(peheader STREQUAL "50450000a601")
  491. set(ARCHITECTURE_ID "SH4")
  492. elseif(peheader STREQUAL "50450000a801")
  493. set(ARCHITECTURE_ID "SH5")
  494. endif()
  495. endif()
  496. # Check if a valid compiler and platform were found.
  497. if(COMPILER_ID AND NOT COMPILER_ID_TWICE)
  498. set(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
  499. set(CMAKE_${lang}_PLATFORM_ID "${PLATFORM_ID}")
  500. set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
  501. set(CMAKE_${lang}_COMPILER_VERSION "${COMPILER_VERSION}")
  502. set(CMAKE_${lang}_SIMULATE_ID "${SIMULATE_ID}")
  503. set(CMAKE_${lang}_SIMULATE_VERSION "${SIMULATE_VERSION}")
  504. endif()
  505. # Check the compiler identification string.
  506. if(CMAKE_${lang}_COMPILER_ID)
  507. # The compiler identification was found.
  508. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  509. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
  510. "${file}\"\n\n")
  511. else()
  512. # The compiler identification could not be found.
  513. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  514. "The ${lang} compiler identification could not be found in \""
  515. "${file}\"\n\n")
  516. endif()
  517. endif()
  518. # try to figure out the executable format: ELF, COFF, Mach-O
  519. if(NOT CMAKE_EXECUTABLE_FORMAT)
  520. file(READ ${file} CMAKE_EXECUTABLE_MAGIC LIMIT 4 HEX)
  521. # ELF files start with 0x7f"ELF"
  522. if("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
  523. set(CMAKE_EXECUTABLE_FORMAT "ELF" CACHE INTERNAL "Executable file format")
  524. endif()
  525. # # COFF (.exe) files start with "MZ"
  526. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
  527. # set(CMAKE_EXECUTABLE_FORMAT "COFF" CACHE STRING "Executable file format")
  528. # endif()
  529. #
  530. # # Mach-O files start with CAFEBABE or FEEDFACE, according to http://radio.weblogs.com/0100490/2003/01/28.html
  531. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe")
  532. # set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  533. # endif()
  534. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface")
  535. # set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  536. # endif()
  537. endif()
  538. if(NOT DEFINED CMAKE_EXECUTABLE_FORMAT)
  539. set(CMAKE_EXECUTABLE_FORMAT)
  540. endif()
  541. # Return the information extracted.
  542. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  543. set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  544. set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  545. PARENT_SCOPE)
  546. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
  547. set(CMAKE_${lang}_COMPILER_WRAPPER "${COMPILER_WRAPPER}" PARENT_SCOPE)
  548. set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE)
  549. set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE)
  550. set(CMAKE_EXECUTABLE_FORMAT "${CMAKE_EXECUTABLE_FORMAT}" PARENT_SCOPE)
  551. set(COMPILER_QNXNTO "${COMPILER_QNXNTO}" PARENT_SCOPE)
  552. set(CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT "${CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT}" PARENT_SCOPE)
  553. endfunction()
  554. #-----------------------------------------------------------------------------
  555. # Function to query the compiler vendor.
  556. # This uses a table with entries of the form
  557. # list(APPEND CMAKE_${lang}_COMPILER_ID_VENDORS ${vendor})
  558. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor} -some-vendor-flag)
  559. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor} "Some Vendor Output")
  560. # We try running the compiler with the flag for each vendor and
  561. # matching its regular expression in the output.
  562. function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang)
  563. if(NOT CMAKE_${lang}_COMPILER_ID_DIR)
  564. # We get here when this function is called not from within CMAKE_DETERMINE_COMPILER_ID()
  565. # This is done e.g. for detecting the compiler ID for assemblers.
  566. # Compute the directory in which to run the test and Create a clean working directory.
  567. set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_PLATFORM_INFO_DIR}/CompilerId${lang})
  568. file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  569. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  570. endif()
  571. foreach(vendor ${CMAKE_${lang}_COMPILER_ID_VENDORS})
  572. set(flags ${CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor}})
  573. set(regex ${CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor}})
  574. execute_process(
  575. COMMAND "${CMAKE_${lang}_COMPILER}"
  576. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  577. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  578. ${flags}
  579. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  580. OUTPUT_VARIABLE output ERROR_VARIABLE output
  581. RESULT_VARIABLE result
  582. TIMEOUT 10
  583. )
  584. if("${output}" MATCHES "${regex}")
  585. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  586. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  587. "matched \"${regex}\":\n${output}")
  588. set(CMAKE_${lang}_COMPILER_ID "${vendor}" PARENT_SCOPE)
  589. break()
  590. else()
  591. if("${result}" MATCHES "timeout")
  592. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  593. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  594. "terminated after 10 s due to timeout.")
  595. else()
  596. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  597. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  598. "did not match \"${regex}\":\n${output}")
  599. endif()
  600. endif()
  601. endforeach()
  602. endfunction()
  603. function(CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX lang)
  604. # Run this MSVC-compatible compiler to detect what the /showIncludes
  605. # option displays. We can use a C source even with the C++ compiler
  606. # because MSVC-compatible compilers handle both and show the same output.
  607. set(showdir ${CMAKE_BINARY_DIR}/CMakeFiles/ShowIncludes)
  608. file(WRITE ${showdir}/foo.h "\n")
  609. file(WRITE ${showdir}/main.c "#include \"foo.h\" \nint main(){}\n")
  610. execute_process(
  611. COMMAND "${CMAKE_${lang}_COMPILER}"
  612. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  613. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  614. /nologo /showIncludes /c main.c
  615. WORKING_DIRECTORY ${showdir}
  616. OUTPUT_VARIABLE out
  617. ERROR_VARIABLE err
  618. RESULT_VARIABLE res
  619. )
  620. if(res EQUAL 0 AND "${out}" MATCHES "(^|\n)([^:\n]*:[^:\n]*:[ \t]*)")
  621. set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "${CMAKE_MATCH_2}" PARENT_SCOPE)
  622. else()
  623. set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "" PARENT_SCOPE)
  624. endif()
  625. endfunction()