1
0

CMakeDetermineCompilerId.cmake 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. macro(__determine_compiler_id_test testflags_var userflags_var)
  4. separate_arguments(testflags UNIX_COMMAND "${${testflags_var}}")
  5. CMAKE_DETERMINE_COMPILER_ID_BUILD("${lang}" "${testflags}" "${${userflags_var}}" "${src}")
  6. CMAKE_DETERMINE_COMPILER_ID_MATCH_VENDOR("${lang}" "${COMPILER_${lang}_PRODUCED_OUTPUT}")
  7. if(NOT CMAKE_${lang}_COMPILER_ID)
  8. foreach(file ${COMPILER_${lang}_PRODUCED_FILES})
  9. CMAKE_DETERMINE_COMPILER_ID_CHECK("${lang}" "${CMAKE_${lang}_COMPILER_ID_DIR}/${file}" "${src}")
  10. endforeach()
  11. endif()
  12. endmacro()
  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. elseif(DEFINED ENV{${flagvar}})
  24. set(CMAKE_${lang}_COMPILER_ID_FLAGS $ENV{${flagvar}})
  25. else(CMAKE_${lang}_FLAGS_INIT)
  26. set(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS_INIT})
  27. endif()
  28. string(REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}")
  29. # Compute the directory in which to run the test.
  30. set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_PLATFORM_INFO_DIR}/CompilerId${lang})
  31. # If we REQUIRE_SUCCESS, i.e. TEST_FLAGS_FIRST has the correct flags, we still need to
  32. # try two combinations: with COMPILER_ID_FLAGS (from user) and without (see issue #21869).
  33. if(CMAKE_${lang}_COMPILER_ID_REQUIRE_SUCCESS)
  34. # If there COMPILER_ID_FLAGS is empty we can error for the first invocation.
  35. if("${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}" STREQUAL "")
  36. set(__compiler_id_require_success TRUE)
  37. endif()
  38. foreach(userflags "${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}" "")
  39. set(testflags "${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS_FIRST}")
  40. __determine_compiler_id_test(testflags userflags)
  41. if(CMAKE_${lang}_COMPILER_ID)
  42. break()
  43. endif()
  44. set(__compiler_id_require_success TRUE)
  45. endforeach()
  46. else()
  47. # Try building with no extra flags and then try each set
  48. # of helper flags. Stop when the compiler is identified.
  49. foreach(userflags "${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}" "")
  50. foreach(testflags ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS_FIRST} "" ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS})
  51. __determine_compiler_id_test(testflags userflags)
  52. if(CMAKE_${lang}_COMPILER_ID)
  53. break()
  54. endif()
  55. endforeach()
  56. if(CMAKE_${lang}_COMPILER_ID)
  57. break()
  58. endif()
  59. endforeach()
  60. endif()
  61. # Check if compiler id detection gave us the compiler tool.
  62. if(CMAKE_${lang}_COMPILER_ID_TOOL)
  63. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER_ID_TOOL}")
  64. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER_ID_TOOL}" PARENT_SCOPE)
  65. elseif(NOT CMAKE_${lang}_COMPILER)
  66. set(CMAKE_${lang}_COMPILER "CMAKE_${lang}_COMPILER-NOTFOUND" PARENT_SCOPE)
  67. endif()
  68. # If the compiler is still unknown, try to query its vendor.
  69. if(CMAKE_${lang}_COMPILER AND NOT CMAKE_${lang}_COMPILER_ID)
  70. foreach(userflags "${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}" "")
  71. CMAKE_DETERMINE_COMPILER_ID_VENDOR(${lang} "${userflags}")
  72. endforeach()
  73. endif()
  74. # If the compiler is still unknown, fallback to GHS
  75. if(NOT CMAKE_${lang}_COMPILER_ID AND "${CMAKE_GENERATOR}" MATCHES "Green Hills MULTI")
  76. set(CMAKE_${lang}_COMPILER_ID GHS)
  77. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  78. "The ${lang} compiler identification is falling back to GHS.\n\n")
  79. endif()
  80. # CUDA < 7.5 is missing version macros
  81. if(lang STREQUAL "CUDA"
  82. AND CMAKE_${lang}_COMPILER_ID STREQUAL "NVIDIA"
  83. AND NOT CMAKE_${lang}_COMPILER_VERSION)
  84. execute_process(
  85. COMMAND "${CMAKE_${lang}_COMPILER}"
  86. --version
  87. OUTPUT_VARIABLE output ERROR_VARIABLE output
  88. RESULT_VARIABLE result
  89. TIMEOUT 10
  90. )
  91. if(output MATCHES [=[ V([0-9]+)\.([0-9]+)\.([0-9]+)]=])
  92. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
  93. endif()
  94. endif()
  95. # For Swift we need to explicitly query the version.
  96. if(lang STREQUAL "Swift"
  97. AND CMAKE_${lang}_COMPILER
  98. AND NOT CMAKE_${lang}_COMPILER_VERSION)
  99. execute_process(
  100. COMMAND "${CMAKE_${lang}_COMPILER}"
  101. -version
  102. OUTPUT_VARIABLE output ERROR_VARIABLE output
  103. RESULT_VARIABLE result
  104. TIMEOUT 10
  105. )
  106. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  107. "Running the ${lang} compiler: \"${CMAKE_${lang}_COMPILER}\" -version\n"
  108. "${output}\n"
  109. )
  110. if(output MATCHES [[Swift version ([0-9]+\.[0-9]+(\.[0-9]+)?)]])
  111. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_MATCH_1}")
  112. if(NOT CMAKE_${lang}_COMPILER_ID)
  113. set(CMAKE_Swift_COMPILER_ID "Apple")
  114. endif()
  115. endif()
  116. endif()
  117. # For ISPC we need to explicitly query the version.
  118. if(lang STREQUAL "ISPC"
  119. AND CMAKE_${lang}_COMPILER
  120. AND NOT CMAKE_${lang}_COMPILER_VERSION)
  121. execute_process(
  122. COMMAND "${CMAKE_${lang}_COMPILER}"
  123. --version
  124. OUTPUT_VARIABLE output ERROR_VARIABLE output
  125. RESULT_VARIABLE result
  126. TIMEOUT 10
  127. )
  128. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  129. "Running the ${lang} compiler: \"${CMAKE_${lang}_COMPILER}\" -version\n"
  130. "${output}\n"
  131. )
  132. if(output MATCHES [[ISPC\), ([0-9]+\.[0-9]+(\.[0-9]+)?)]])
  133. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_MATCH_1}")
  134. endif()
  135. endif()
  136. if (COMPILER_QNXNTO AND CMAKE_${lang}_COMPILER_ID STREQUAL "GNU")
  137. execute_process(
  138. COMMAND "${CMAKE_${lang}_COMPILER}"
  139. -V
  140. OUTPUT_VARIABLE output ERROR_VARIABLE output
  141. RESULT_VARIABLE result
  142. TIMEOUT 10
  143. )
  144. if (output MATCHES "targets available")
  145. set(CMAKE_${lang}_COMPILER_ID QCC)
  146. # http://community.qnx.com/sf/discussion/do/listPosts/projects.community/discussion.qnx_momentics_community_support.topc3555?_pagenum=2
  147. # The qcc driver does not itself have a version.
  148. endif()
  149. endif()
  150. # The Fujitsu compiler does not always convey version information through
  151. # preprocessor symbols so we extract through command line info
  152. if (CMAKE_${lang}_COMPILER_ID STREQUAL "Fujitsu")
  153. if(NOT CMAKE_${lang}_COMPILER_VERSION)
  154. execute_process(
  155. COMMAND "${CMAKE_${lang}_COMPILER}" -V
  156. OUTPUT_VARIABLE output
  157. ERROR_VARIABLE output
  158. RESULT_VARIABLE result
  159. TIMEOUT 10
  160. )
  161. if (result EQUAL 0)
  162. if (output MATCHES [[Fujitsu [^ ]* Compiler ([0-9]+\.[0-9]+\.[0-9]+)]])
  163. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_MATCH_1}")
  164. endif()
  165. endif()
  166. endif()
  167. endif()
  168. # if the format is unknown after all files have been checked, put "Unknown" in the cache
  169. if(NOT CMAKE_EXECUTABLE_FORMAT)
  170. set(CMAKE_EXECUTABLE_FORMAT "Unknown" CACHE INTERNAL "Executable file format")
  171. endif()
  172. if((CMAKE_GENERATOR MATCHES "^Ninja"
  173. OR ((NOT DEFINED CMAKE_DEPENDS_USE_COMPILER OR CMAKE_DEPENDS_USE_COMPILER)
  174. AND CMAKE_GENERATOR MATCHES "Makefiles|WMake"))
  175. AND MSVC_${lang}_ARCHITECTURE_ID)
  176. foreach(userflags "${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}" "")
  177. CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX(${lang} "${userflags}")
  178. endforeach()
  179. else()
  180. set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "")
  181. endif()
  182. set(_variant "")
  183. if("x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xClang"
  184. OR "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xIntelLLVM")
  185. if("x${CMAKE_${lang}_SIMULATE_ID}" STREQUAL "xMSVC")
  186. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  187. set(CMAKE_${lang}_COMPILER_FRONTEND_VARIANT "MSVC")
  188. else()
  189. # Test whether an MSVC-like command-line option works.
  190. execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" -?
  191. RESULT_VARIABLE _clang_result
  192. OUTPUT_VARIABLE _clang_stdout
  193. ERROR_VARIABLE _clang_stderr)
  194. if(_clang_result EQUAL 0)
  195. set(CMAKE_${lang}_COMPILER_FRONTEND_VARIANT "MSVC")
  196. else()
  197. set(CMAKE_${lang}_COMPILER_FRONTEND_VARIANT "GNU")
  198. endif()
  199. endif()
  200. set(_variant " with ${CMAKE_${lang}_COMPILER_FRONTEND_VARIANT}-like command-line")
  201. else()
  202. set(CMAKE_${lang}_COMPILER_FRONTEND_VARIANT "GNU")
  203. endif()
  204. else()
  205. set(CMAKE_${lang}_COMPILER_FRONTEND_VARIANT "")
  206. endif()
  207. # Display the final identification result.
  208. if(CMAKE_${lang}_COMPILER_ID)
  209. if(CMAKE_${lang}_COMPILER_VERSION)
  210. set(_version " ${CMAKE_${lang}_COMPILER_VERSION}")
  211. else()
  212. set(_version "")
  213. endif()
  214. if(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID AND "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xIAR")
  215. set(_archid " ${CMAKE_${lang}_COMPILER_ARCHITECTURE_ID}")
  216. else()
  217. set(_archid "")
  218. endif()
  219. message(STATUS "The ${lang} compiler identification is "
  220. "${CMAKE_${lang}_COMPILER_ID}${_archid}${_version}${_variant}")
  221. unset(_archid)
  222. unset(_version)
  223. unset(_variant)
  224. else()
  225. message(STATUS "The ${lang} compiler identification is unknown")
  226. endif()
  227. if(lang STREQUAL "Fortran" AND CMAKE_${lang}_COMPILER_ID STREQUAL "XL")
  228. set(CMAKE_${lang}_XL_CPP "${CMAKE_${lang}_COMPILER_ID_CPP}" PARENT_SCOPE)
  229. endif()
  230. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  231. set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  232. set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "${CMAKE_${lang}_COMPILER_ARCHITECTURE_ID}" PARENT_SCOPE)
  233. set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  234. PARENT_SCOPE)
  235. set(CMAKE_${lang}_XCODE_ARCHS "${CMAKE_${lang}_XCODE_ARCHS}" PARENT_SCOPE)
  236. set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "${CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX}" PARENT_SCOPE)
  237. set(CMAKE_${lang}_COMPILER_FRONTEND_VARIANT "${CMAKE_${lang}_COMPILER_FRONTEND_VARIANT}" PARENT_SCOPE)
  238. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
  239. set(CMAKE_${lang}_COMPILER_VERSION_INTERNAL "${CMAKE_${lang}_COMPILER_VERSION_INTERNAL}" PARENT_SCOPE)
  240. set(CMAKE_${lang}_COMPILER_WRAPPER "${CMAKE_${lang}_COMPILER_WRAPPER}" PARENT_SCOPE)
  241. set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE)
  242. set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE)
  243. set(CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT "${CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT}" PARENT_SCOPE)
  244. set(CMAKE_${lang}_COMPILER_PRODUCED_OUTPUT "${COMPILER_${lang}_PRODUCED_OUTPUT}" PARENT_SCOPE)
  245. set(CMAKE_${lang}_COMPILER_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE)
  246. endfunction()
  247. include(CMakeCompilerIdDetection)
  248. #-----------------------------------------------------------------------------
  249. # Function to write the compiler id source file.
  250. function(CMAKE_DETERMINE_COMPILER_ID_WRITE lang src)
  251. find_file(src_in ${src}.in PATHS ${CMAKE_ROOT}/Modules ${CMAKE_MODULE_PATH} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
  252. file(READ ${src_in} ID_CONTENT_IN)
  253. compiler_id_detection(CMAKE_${lang}_COMPILER_ID_CONTENT ${lang}
  254. ID_STRING
  255. VERSION_STRINGS
  256. PLATFORM_DEFAULT_COMPILER
  257. )
  258. unset(src_in CACHE)
  259. string(CONFIGURE "${ID_CONTENT_IN}" ID_CONTENT_OUT @ONLY)
  260. file(WRITE ${CMAKE_${lang}_COMPILER_ID_DIR}/${src} "${ID_CONTENT_OUT}")
  261. endfunction()
  262. #-----------------------------------------------------------------------------
  263. # Function to build the compiler id source file and look for output
  264. # files.
  265. function(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags userflags src)
  266. # Create a clean working directory.
  267. file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  268. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  269. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}/tmp)
  270. CMAKE_DETERMINE_COMPILER_ID_WRITE("${lang}" "${src}")
  271. # Construct a description of this test case.
  272. set(COMPILER_DESCRIPTION
  273. "Compiler: ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1}
  274. Build flags: ${userflags}
  275. Id flags: ${testflags} ${CMAKE_${lang}_COMPILER_ID_FLAGS_ALWAYS}
  276. ")
  277. # Compile the compiler identification source.
  278. if("${CMAKE_GENERATOR}" MATCHES "Visual Studio ([0-9]+)")
  279. set(vs_version ${CMAKE_MATCH_1})
  280. set(id_platform ${CMAKE_VS_PLATFORM_NAME})
  281. set(id_lang "${lang}")
  282. set(id_PostBuildEvent_Command "")
  283. if(CMAKE_VS_PLATFORM_TOOLSET MATCHES "^[Ll][Ll][Vv][Mm](_v[0-9]+(_xp)?)?$")
  284. set(id_cl_var "ClangClExecutable")
  285. elseif(CMAKE_VS_PLATFORM_TOOLSET MATCHES "^[Cc][Ll][Aa][Nn][Gg]([Cc][Ll]$|_[0-9])")
  286. set(id_cl "$(CLToolExe)")
  287. elseif(CMAKE_VS_PLATFORM_TOOLSET MATCHES "v[0-9]+_clang_.*")
  288. set(id_cl clang.exe)
  289. # Executable names have choosen according documentation
  290. # URL: (https://software.intel.com/content/www/us/en/develop/documentation/get-started-with-dpcpp-compiler/top.html#top_GUID-A9B4C91D-97AC-450D-9742-9D895BC8AEE1)
  291. elseif(CMAKE_VS_PLATFORM_TOOLSET MATCHES "Intel")
  292. if(CMAKE_VS_PLATFORM_TOOLSET MATCHES "DPC\\+\\+ Compiler")
  293. set(id_cl dpcpp.exe)
  294. elseif(CMAKE_VS_PLATFORM_TOOLSET MATCHES "C\\+\\+ Compiler 2021")
  295. set(id_cl icx.exe)
  296. elseif(CMAKE_VS_PLATFORM_TOOLSET MATCHES "C\\+\\+ Compiler")
  297. set(id_cl icl.exe)
  298. endif()
  299. else()
  300. set(id_cl cl.exe)
  301. endif()
  302. if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
  303. set(v NsightTegra)
  304. set(ext vcxproj)
  305. if(lang STREQUAL CXX)
  306. set(id_gcc g++)
  307. set(id_clang clang++)
  308. else()
  309. set(id_gcc gcc)
  310. set(id_clang clang)
  311. endif()
  312. elseif(lang STREQUAL Fortran)
  313. set(v Intel)
  314. set(ext vfproj)
  315. set(id_cl ifort.exe)
  316. elseif(lang STREQUAL CSharp)
  317. set(v 10)
  318. set(ext csproj)
  319. set(id_cl csc.exe)
  320. elseif(NOT "${vs_version}" VERSION_LESS 10)
  321. set(v 10)
  322. set(ext vcxproj)
  323. else()
  324. set(id_version ${vs_version}.00)
  325. set(v 7)
  326. set(ext vcproj)
  327. endif()
  328. if(CMAKE_VS_PLATFORM_TOOLSET)
  329. if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
  330. set(id_toolset "<NdkToolchainVersion>${CMAKE_VS_PLATFORM_TOOLSET}</NdkToolchainVersion>")
  331. else()
  332. set(id_toolset "<PlatformToolset>${CMAKE_VS_PLATFORM_TOOLSET}</PlatformToolset>")
  333. if(CMAKE_VS_PLATFORM_TOOLSET_VERSION)
  334. set(id_sep "\\")
  335. if(CMAKE_VS_PLATFORM_TOOLSET_VERSION VERSION_GREATER_EQUAL "14.20")
  336. if(EXISTS "${CMAKE_GENERATOR_INSTANCE}/VC/Auxiliary/Build.${CMAKE_VS_PLATFORM_TOOLSET_VERSION}/Microsoft.VCToolsVersion.${CMAKE_VS_PLATFORM_TOOLSET_VERSION}.props")
  337. set(id_sep ".")
  338. endif()
  339. endif()
  340. set(id_toolset_version_props "<Import Project=\"${CMAKE_GENERATOR_INSTANCE}\\VC\\Auxiliary\\Build${id_sep}${CMAKE_VS_PLATFORM_TOOLSET_VERSION}\\Microsoft.VCToolsVersion.${CMAKE_VS_PLATFORM_TOOLSET_VERSION}.props\" />")
  341. unset(id_sep)
  342. endif()
  343. endif()
  344. else()
  345. set(id_toolset "")
  346. set(id_toolset_version_props "")
  347. endif()
  348. if(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE)
  349. set(id_PreferredToolArchitecture "<PreferredToolArchitecture>${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}</PreferredToolArchitecture>")
  350. else()
  351. set(id_PreferredToolArchitecture "")
  352. endif()
  353. if(CMAKE_SYSTEM_NAME STREQUAL "WindowsPhone")
  354. set(id_keyword "Win32Proj")
  355. set(id_system "<ApplicationType>Windows Phone</ApplicationType>")
  356. elseif(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
  357. set(id_keyword "Win32Proj")
  358. set(id_system "<ApplicationType>Windows Store</ApplicationType>")
  359. elseif(CMAKE_SYSTEM_NAME STREQUAL "Android")
  360. set(id_keyword "Android")
  361. set(id_system "<ApplicationType>Android</ApplicationType>")
  362. else()
  363. set(id_keyword "Win32Proj")
  364. set(id_system "")
  365. endif()
  366. if(id_keyword STREQUAL "Android")
  367. if(CMAKE_GENERATOR MATCHES "Visual Studio 14")
  368. set(id_system_version "<ApplicationTypeRevision>2.0</ApplicationTypeRevision>")
  369. elseif(CMAKE_GENERATOR MATCHES "Visual Studio 1[56]")
  370. set(id_system_version "<ApplicationTypeRevision>3.0</ApplicationTypeRevision>")
  371. else()
  372. set(id_system_version "")
  373. endif()
  374. elseif(id_system AND CMAKE_SYSTEM_VERSION MATCHES "^([0-9]+\\.[0-9]+)")
  375. set(id_system_version "<ApplicationTypeRevision>${CMAKE_MATCH_1}</ApplicationTypeRevision>")
  376. else()
  377. set(id_system_version "")
  378. endif()
  379. if(id_keyword STREQUAL "Android")
  380. set(id_config_type "DynamicLibrary")
  381. else()
  382. set(id_config_type "Application")
  383. endif()
  384. if(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION)
  385. set(id_WindowsTargetPlatformVersion "<WindowsTargetPlatformVersion>${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}</WindowsTargetPlatformVersion>")
  386. endif()
  387. if(CMAKE_VS_PLATFORM_TOOLSET_VCTARGETS_CUSTOM_DIR)
  388. set(id_ToolsetVCTargetsDir "<VCTargetsPath>${CMAKE_VS_PLATFORM_TOOLSET_VCTARGETS_CUSTOM_DIR}</VCTargetsPath>")
  389. endif()
  390. set(id_CustomGlobals "")
  391. foreach(pair IN LISTS CMAKE_VS_GLOBALS)
  392. if("${pair}" MATCHES "([^=]+)=(.*)$")
  393. string(APPEND id_CustomGlobals "<${CMAKE_MATCH_1}>${CMAKE_MATCH_2}</${CMAKE_MATCH_1}>\n ")
  394. endif()
  395. endforeach()
  396. if(id_keyword STREQUAL "Android")
  397. set(id_WindowsSDKDesktopARMSupport "")
  398. elseif(id_platform STREQUAL "ARM64")
  399. set(id_WindowsSDKDesktopARMSupport "<WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>")
  400. elseif(id_platform STREQUAL "ARM")
  401. set(id_WindowsSDKDesktopARMSupport "<WindowsSDKDesktopARMSupport>true</WindowsSDKDesktopARMSupport>")
  402. else()
  403. set(id_WindowsSDKDesktopARMSupport "")
  404. endif()
  405. if(CMAKE_VS_WINCE_VERSION)
  406. set(id_entrypoint "mainACRTStartup")
  407. if("${vs_version}" VERSION_LESS 9)
  408. set(id_subsystem 9)
  409. else()
  410. set(id_subsystem 8)
  411. endif()
  412. else()
  413. set(id_subsystem 1)
  414. endif()
  415. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  416. set(id_src "${src}")
  417. set(id_compile "ClCompile")
  418. if(id_cl_var)
  419. set(id_PostBuildEvent_Command "echo CMAKE_${lang}_COMPILER=$(${id_cl_var})")
  420. else()
  421. set(id_PostBuildEvent_Command "for %%i in (${id_cl}) do %40echo CMAKE_${lang}_COMPILER=%%~$PATH:i")
  422. endif()
  423. set(id_Import_props "")
  424. set(id_Import_targets "")
  425. set(id_ItemDefinitionGroup_entry "")
  426. set(id_Link_AdditionalDependencies "")
  427. if(lang STREQUAL CUDA)
  428. if(NOT CMAKE_VS_PLATFORM_TOOLSET_CUDA)
  429. message(FATAL_ERROR "No CUDA toolset found.")
  430. endif()
  431. set(cuda_tools "CUDA ${CMAKE_VS_PLATFORM_TOOLSET_CUDA}")
  432. set(id_compile "CudaCompile")
  433. if(CMAKE_VS_PLATFORM_NAME STREQUAL x64)
  434. set(cuda_target "<TargetMachinePlatform>64</TargetMachinePlatform>")
  435. endif()
  436. foreach(arch ${CMAKE_CUDA_ARCHITECTURES})
  437. string(REGEX MATCH "[0-9]+" arch_name "${arch}")
  438. string(APPEND cuda_codegen "compute_${arch_name},sm_${arch_name};")
  439. endforeach()
  440. set(id_ItemDefinitionGroup_entry "<CudaCompile>${cuda_target}<AdditionalOptions>%(AdditionalOptions)-v</AdditionalOptions><CodeGeneration>${cuda_codegen}</CodeGeneration></CudaCompile>")
  441. set(id_PostBuildEvent_Command [[echo CMAKE_CUDA_COMPILER=$(CudaToolkitBinDir)\nvcc.exe]])
  442. if(CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR)
  443. # check for legacy cuda custom toolkit folder structure
  444. if(EXISTS ${CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR}nvcc)
  445. set(id_CudaToolkitCustomDir "<CudaToolkitCustomDir>${CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR}nvcc</CudaToolkitCustomDir>")
  446. else()
  447. set(id_CudaToolkitCustomDir "<CudaToolkitCustomDir>${CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR}</CudaToolkitCustomDir>")
  448. endif()
  449. if(EXISTS ${CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR}CUDAVisualStudioIntegration)
  450. string(CONCAT id_Import_props "<Import Project=\"${CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR}CUDAVisualStudioIntegration\\extras\\visual_studio_integration\\MSBuildExtensions\\${cuda_tools}.props\" />")
  451. string(CONCAT id_Import_targets "<Import Project=\"${CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR}CUDAVisualStudioIntegration\\extras\\visual_studio_integration\\MSBuildExtensions\\${cuda_tools}.targets\" />")
  452. else()
  453. string(CONCAT id_Import_props "<Import Project=\"${CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR}\\extras\\visual_studio_integration\\MSBuildExtensions\\${cuda_tools}.props\" />")
  454. string(CONCAT id_Import_targets "<Import Project=\"${CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR}\\extras\\visual_studio_integration\\MSBuildExtensions\\${cuda_tools}.targets\" />")
  455. endif()
  456. else()
  457. string(CONCAT id_Import_props [[<Import Project="$(VCTargetsPath)\BuildCustomizations\]] "${cuda_tools}" [[.props" />]])
  458. string(CONCAT id_Import_targets [[<Import Project="$(VCTargetsPath)\BuildCustomizations\]] "${cuda_tools}" [[.targets" />]])
  459. endif()
  460. if(CMAKE_CUDA_FLAGS MATCHES "(^| )-cudart +shared( |$)")
  461. set(id_Link_AdditionalDependencies "<AdditionalDependencies>cudart.lib</AdditionalDependencies>")
  462. else()
  463. set(id_Link_AdditionalDependencies "<AdditionalDependencies>cudart_static.lib</AdditionalDependencies>")
  464. endif()
  465. endif()
  466. configure_file(${CMAKE_ROOT}/Modules/CompilerId/VS-${v}.${ext}.in
  467. ${id_dir}/CompilerId${lang}.${ext} @ONLY)
  468. if(CMAKE_VS_MSBUILD_COMMAND AND NOT lang STREQUAL "Fortran")
  469. set(command "${CMAKE_VS_MSBUILD_COMMAND}" "CompilerId${lang}.${ext}"
  470. "/p:Configuration=Debug" "/p:Platform=${id_platform}" "/p:VisualStudioVersion=${vs_version}.0"
  471. )
  472. elseif(CMAKE_VS_DEVENV_COMMAND)
  473. set(command "${CMAKE_VS_DEVENV_COMMAND}" "CompilerId${lang}.${ext}" "/build" "Debug")
  474. else()
  475. set(command "")
  476. endif()
  477. if(command)
  478. execute_process(
  479. COMMAND ${command}
  480. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  481. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  482. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  483. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  484. )
  485. else()
  486. set(CMAKE_${lang}_COMPILER_ID_RESULT 1)
  487. set(CMAKE_${lang}_COMPILER_ID_OUTPUT "VS environment not known to support ${lang}")
  488. endif()
  489. # Match the compiler location line printed out.
  490. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "CMAKE_${lang}_COMPILER=([^%\r\n]+)[\r\n]")
  491. # Strip VS diagnostic output from the end of the line.
  492. string(REGEX REPLACE " \\(TaskId:[0-9]*\\)$" "" _comp "${CMAKE_MATCH_1}")
  493. if(EXISTS "${_comp}")
  494. file(TO_CMAKE_PATH "${_comp}" _comp)
  495. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  496. endif()
  497. endif()
  498. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  499. set(id_lang "${lang}")
  500. set(id_type ${CMAKE_${lang}_COMPILER_XCODE_TYPE})
  501. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  502. set(id_src "${src}")
  503. if(CMAKE_XCODE_PLATFORM_TOOLSET)
  504. set(id_toolset "GCC_VERSION = ${CMAKE_XCODE_PLATFORM_TOOLSET};")
  505. else()
  506. set(id_toolset "")
  507. endif()
  508. if("${lang}" STREQUAL "Swift")
  509. if(CMAKE_Swift_LANGUAGE_VERSION)
  510. set(id_lang_version "SWIFT_VERSION = ${CMAKE_Swift_LANGUAGE_VERSION};")
  511. elseif(XCODE_VERSION VERSION_GREATER_EQUAL 10.2)
  512. set(id_lang_version "SWIFT_VERSION = 4.0;")
  513. elseif(XCODE_VERSION VERSION_GREATER_EQUAL 8.3)
  514. set(id_lang_version "SWIFT_VERSION = 3.0;")
  515. else()
  516. set(id_lang_version "SWIFT_VERSION = 2.3;")
  517. endif()
  518. else()
  519. set(id_lang_version "")
  520. endif()
  521. if(CMAKE_OSX_DEPLOYMENT_TARGET)
  522. set(id_deployment_target
  523. "MACOSX_DEPLOYMENT_TARGET = \"${CMAKE_OSX_DEPLOYMENT_TARGET}\";")
  524. else()
  525. set(id_deployment_target "")
  526. endif()
  527. set(id_product_type "com.apple.product-type.tool")
  528. if(CMAKE_OSX_SYSROOT)
  529. set(id_sdkroot "SDKROOT = \"${CMAKE_OSX_SYSROOT}\";")
  530. if(CMAKE_OSX_SYSROOT MATCHES "(^|/)[Ii][Pp][Hh][Oo][Nn][Ee]" OR
  531. CMAKE_OSX_SYSROOT MATCHES "(^|/)[Aa][Pp][Pp][Ll][Ee][Tt][Vv]")
  532. set(id_product_type "com.apple.product-type.bundle.unit-test")
  533. elseif(CMAKE_OSX_SYSROOT MATCHES "(^|/)[Ww][Aa][Tt][Cc][Hh]")
  534. set(id_product_type "com.apple.product-type.framework")
  535. endif()
  536. else()
  537. set(id_sdkroot "")
  538. endif()
  539. set(id_clang_cxx_library "")
  540. set(stdlib_regex "(^| )(-stdlib=)([^ ]+)( |$)")
  541. string(REGEX MATCHALL "${stdlib_regex}" all_stdlib_matches "${CMAKE_CXX_FLAGS}")
  542. if(all_stdlib_matches)
  543. list(GET all_stdlib_matches "-1" last_stdlib_match)
  544. if(last_stdlib_match MATCHES "${stdlib_regex}")
  545. set(id_clang_cxx_library "CLANG_CXX_LIBRARY = \"${CMAKE_MATCH_3}\";")
  546. endif()
  547. endif()
  548. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_SYSROOT MATCHES "^$|[Mm][Aa][Cc][Oo][Ss]")
  549. # When targeting macOS, use only the host architecture.
  550. if (_CMAKE_APPLE_ARCHS_DEFAULT)
  551. set(id_archs "ARCHS = \"${_CMAKE_APPLE_ARCHS_DEFAULT}\";")
  552. set(id_arch_active "ONLY_ACTIVE_ARCH = NO;")
  553. else()
  554. set(id_archs [[ARCHS = "$(NATIVE_ARCH_ACTUAL)";]])
  555. set(id_arch_active "ONLY_ACTIVE_ARCH = YES;")
  556. endif()
  557. else()
  558. set(id_archs "")
  559. set(id_arch_active "ONLY_ACTIVE_ARCH = YES;")
  560. endif()
  561. configure_file(${CMAKE_ROOT}/Modules/CompilerId/Xcode-3.pbxproj.in
  562. ${id_dir}/CompilerId${lang}.xcodeproj/project.pbxproj @ONLY)
  563. unset(_ENV_MACOSX_DEPLOYMENT_TARGET)
  564. if(DEFINED ENV{MACOSX_DEPLOYMENT_TARGET})
  565. set(_ENV_MACOSX_DEPLOYMENT_TARGET "$ENV{MACOSX_DEPLOYMENT_TARGET}")
  566. set(ENV{MACOSX_DEPLOYMENT_TARGET} "")
  567. endif()
  568. execute_process(COMMAND xcodebuild
  569. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  570. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  571. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  572. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  573. )
  574. if(DEFINED _ENV_MACOSX_DEPLOYMENT_TARGET)
  575. set(ENV{MACOSX_DEPLOYMENT_TARGET} "${_ENV_MACOSX_DEPLOYMENT_TARGET}")
  576. endif()
  577. if(DEFINED CMAKE_${lang}_COMPILER_ID_TOOL_MATCH_REGEX)
  578. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "${CMAKE_${lang}_COMPILER_ID_TOOL_MATCH_REGEX}")
  579. set(_comp "${CMAKE_MATCH_${CMAKE_${lang}_COMPILER_ID_TOOL_MATCH_INDEX}}")
  580. if(EXISTS "${_comp}")
  581. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  582. endif()
  583. endif()
  584. endif()
  585. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "ARCHS=([^%\r\n]+)[\r\n]")
  586. set(CMAKE_${lang}_XCODE_ARCHS "${CMAKE_MATCH_1}")
  587. separate_arguments(CMAKE_${lang}_XCODE_ARCHS)
  588. set(CMAKE_${lang}_XCODE_ARCHS "${CMAKE_${lang}_XCODE_ARCHS}" PARENT_SCOPE)
  589. endif()
  590. elseif("${CMAKE_GENERATOR}" MATCHES "Green Hills MULTI")
  591. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  592. set(id_src "${src}")
  593. if (GHS_PRIMARY_TARGET)
  594. set(ghs_primary_target "${GHS_PRIMARY_TARGET}")
  595. else()
  596. set(ghs_primary_target "${CMAKE_GENERATOR_PLATFORM}_${GHS_TARGET_PLATFORM}.tgt")
  597. endif()
  598. if ("${GHS_TARGET_PLATFORM}" MATCHES "integrity")
  599. set(bsp_name "macro GHS_BSP=${GHS_BSP_NAME}")
  600. set(os_dir "macro GHS_OS=${GHS_OS_DIR}")
  601. endif()
  602. set(command "${CMAKE_MAKE_PROGRAM}" "-commands" "-top" "GHS_default.gpj")
  603. configure_file(${CMAKE_ROOT}/Modules/CompilerId/GHS_default.gpj.in
  604. ${id_dir}/GHS_default.gpj @ONLY)
  605. configure_file(${CMAKE_ROOT}/Modules/CompilerId/GHS_lib.gpj.in
  606. ${id_dir}/GHS_lib.gpj @ONLY)
  607. execute_process(COMMAND ${command}
  608. WORKING_DIRECTORY ${id_dir}
  609. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  610. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  611. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  612. )
  613. # Match the compiler location line printed out.
  614. set(ghs_toolpath "${CMAKE_MAKE_PROGRAM}")
  615. if(CMAKE_HOST_UNIX)
  616. string(REPLACE "/gbuild" "/" ghs_toolpath ${ghs_toolpath})
  617. else()
  618. string(REPLACE "/gbuild.exe" "/" ghs_toolpath ${ghs_toolpath})
  619. string(REPLACE / "\\\\" ghs_toolpath ${ghs_toolpath})
  620. endif()
  621. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "(${ghs_toolpath}[^ ]*)")
  622. if(CMAKE_HOST_UNIX)
  623. set(_comp "${CMAKE_MATCH_1}")
  624. else()
  625. set(_comp "${CMAKE_MATCH_1}.exe")
  626. endif()
  627. if(EXISTS "${_comp}")
  628. file(TO_CMAKE_PATH "${_comp}" _comp)
  629. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  630. endif()
  631. endif()
  632. else()
  633. execute_process(
  634. COMMAND "${CMAKE_${lang}_COMPILER}"
  635. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  636. ${userflags}
  637. ${testflags}
  638. ${CMAKE_${lang}_COMPILER_ID_FLAGS_ALWAYS}
  639. "${src}"
  640. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  641. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  642. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  643. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  644. )
  645. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "exec: [^\n]*\\((/[^,\n]*/cpp),CMakeFortranCompilerId.F")
  646. set(_cpp "${CMAKE_MATCH_1}")
  647. if(EXISTS "${_cpp}")
  648. set(CMAKE_${lang}_COMPILER_ID_CPP "${_cpp}" PARENT_SCOPE)
  649. endif()
  650. endif()
  651. endif()
  652. # Check the result of compilation.
  653. if(CMAKE_${lang}_COMPILER_ID_RESULT
  654. # Intel Fortran warns and ignores preprocessor lines without /fpp
  655. OR CMAKE_${lang}_COMPILER_ID_OUTPUT MATCHES "Bad # preprocessor line"
  656. )
  657. # Compilation failed.
  658. set(MSG
  659. "Compiling the ${lang} compiler identification source file \"${src}\" failed.
  660. ${COMPILER_DESCRIPTION}
  661. The output was:
  662. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  663. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  664. ")
  665. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log "${MSG}")
  666. # Some languages may know the correct/desired set of flags and want to fail right away if they don't work.
  667. # This is currently only used by CUDA.
  668. if(__compiler_id_require_success)
  669. message(FATAL_ERROR "${MSG}")
  670. endif()
  671. # No output files should be inspected.
  672. set(COMPILER_${lang}_PRODUCED_FILES)
  673. set(COMPILER_${lang}_PRODUCED_OUTPUT)
  674. else()
  675. # Compilation succeeded.
  676. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  677. "Compiling the ${lang} compiler identification source file \"${src}\" succeeded.
  678. ${COMPILER_DESCRIPTION}
  679. The output was:
  680. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  681. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  682. ")
  683. # Find the executable produced by the compiler, try all files in the
  684. # binary dir.
  685. string(REGEX REPLACE "([][])" "[\\1]" _glob_id_dir "${CMAKE_${lang}_COMPILER_ID_DIR}")
  686. file(GLOB files
  687. RELATIVE ${CMAKE_${lang}_COMPILER_ID_DIR}
  688. # normal case
  689. ${_glob_id_dir}/*
  690. # com.apple.package-type.bundle.unit-test
  691. ${_glob_id_dir}/*.xctest/*
  692. # com.apple.product-type.framework
  693. ${_glob_id_dir}/*.framework/*
  694. )
  695. list(REMOVE_ITEM files "${src}")
  696. set(COMPILER_${lang}_PRODUCED_FILES "")
  697. foreach(file ${files})
  698. if(NOT IS_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}/${file})
  699. list(APPEND COMPILER_${lang}_PRODUCED_FILES ${file})
  700. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  701. "Compilation of the ${lang} compiler identification source \""
  702. "${src}\" produced \"${file}\"\n\n")
  703. endif()
  704. endforeach()
  705. if(NOT COMPILER_${lang}_PRODUCED_FILES)
  706. # No executable was found.
  707. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  708. "Compilation of the ${lang} compiler identification source \""
  709. "${src}\" did not produce an executable in \""
  710. "${CMAKE_${lang}_COMPILER_ID_DIR}\".\n\n")
  711. endif()
  712. set(COMPILER_${lang}_PRODUCED_OUTPUT "${CMAKE_${lang}_COMPILER_ID_OUTPUT}")
  713. endif()
  714. # Return the files produced by the compilation.
  715. set(COMPILER_${lang}_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE)
  716. set(COMPILER_${lang}_PRODUCED_OUTPUT "${COMPILER_${lang}_PRODUCED_OUTPUT}" PARENT_SCOPE)
  717. endfunction()
  718. #-----------------------------------------------------------------------------
  719. # Function to extract the compiler id from compiler output.
  720. function(CMAKE_DETERMINE_COMPILER_ID_MATCH_VENDOR lang output)
  721. foreach(vendor ${CMAKE_${lang}_COMPILER_ID_MATCH_VENDORS})
  722. if(output MATCHES "${CMAKE_${lang}_COMPILER_ID_MATCH_VENDOR_REGEX_${vendor}}")
  723. set(CMAKE_${lang}_COMPILER_ID "${vendor}")
  724. endif()
  725. endforeach()
  726. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  727. endfunction()
  728. #-----------------------------------------------------------------------------
  729. # Function to extract the compiler id from an executable.
  730. function(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file)
  731. # Look for a compiler id if not yet known.
  732. if(NOT CMAKE_${lang}_COMPILER_ID)
  733. # Read the compiler identification string from the executable file.
  734. set(COMPILER_ID)
  735. set(COMPILER_VERSION)
  736. set(COMPILER_VERSION_MAJOR 0)
  737. set(COMPILER_VERSION_MINOR 0)
  738. set(COMPILER_VERSION_PATCH 0)
  739. set(COMPILER_VERSION_TWEAK 0)
  740. set(COMPILER_VERSION_INTERNAL "")
  741. set(HAVE_COMPILER_VERSION_MAJOR 0)
  742. set(HAVE_COMPILER_VERSION_MINOR 0)
  743. set(HAVE_COMPILER_VERSION_PATCH 0)
  744. set(HAVE_COMPILER_VERSION_TWEAK 0)
  745. set(COMPILER_WRAPPER)
  746. set(DIGIT_VALUE_1 1)
  747. set(DIGIT_VALUE_2 10)
  748. set(DIGIT_VALUE_3 100)
  749. set(DIGIT_VALUE_4 1000)
  750. set(DIGIT_VALUE_5 10000)
  751. set(DIGIT_VALUE_6 100000)
  752. set(DIGIT_VALUE_7 1000000)
  753. set(DIGIT_VALUE_8 10000000)
  754. set(PLATFORM_ID)
  755. set(ARCHITECTURE_ID)
  756. set(SIMULATE_ID)
  757. set(SIMULATE_VERSION)
  758. foreach(encoding "" "ENCODING;UTF-16LE" "ENCODING;UTF-16BE")
  759. file(STRINGS "${file}" CMAKE_${lang}_COMPILER_ID_STRINGS
  760. LIMIT_COUNT 38 ${encoding}
  761. REGEX ".?I.?N.?F.?O.?:.?[A-Za-z0-9_]+\\[[^]]*\\]")
  762. if(NOT CMAKE_${lang}_COMPILER_ID_STRINGS STREQUAL "")
  763. break()
  764. endif()
  765. endforeach()
  766. # With the IAR Compiler, some strings are found twice, first time as incomplete
  767. # list like "?<Constant "INFO:compiler[IAR]">". Remove the incomplete copies.
  768. list(FILTER CMAKE_${lang}_COMPILER_ID_STRINGS EXCLUDE REGEX "\\?<Constant \\\"")
  769. # The IAR-AVR compiler uses a binary format that places a '6'
  770. # character (0x34) before each character in the string. Strip
  771. # out these characters without removing any legitimate characters.
  772. if(CMAKE_${lang}_COMPILER_ID_STRINGS MATCHES "(.)I.N.F.O.:.")
  773. string(REGEX REPLACE "${CMAKE_MATCH_1}([^;])" "\\1"
  774. CMAKE_${lang}_COMPILER_ID_STRINGS "${CMAKE_${lang}_COMPILER_ID_STRINGS}")
  775. endif()
  776. # Remove arbitrary text that may appear before or after each INFO string.
  777. string(REGEX MATCHALL "INFO:[A-Za-z0-9_]+\\[([^]\"]*)\\]"
  778. CMAKE_${lang}_COMPILER_ID_STRINGS "${CMAKE_${lang}_COMPILER_ID_STRINGS}")
  779. # In C# binaries, some strings are found more than once.
  780. list(REMOVE_DUPLICATES CMAKE_${lang}_COMPILER_ID_STRINGS)
  781. set(COMPILER_ID_TWICE)
  782. foreach(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  783. if("${info}" MATCHES "INFO:compiler\\[([^]\"]*)\\]")
  784. if(COMPILER_ID)
  785. set(COMPILER_ID_TWICE 1)
  786. endif()
  787. set(COMPILER_ID "${CMAKE_MATCH_1}")
  788. endif()
  789. if("${info}" MATCHES "INFO:platform\\[([^]\"]*)\\]")
  790. set(PLATFORM_ID "${CMAKE_MATCH_1}")
  791. endif()
  792. if("${info}" MATCHES "INFO:arch\\[([^]\"]*)\\]")
  793. set(ARCHITECTURE_ID "${CMAKE_MATCH_1}")
  794. endif()
  795. if("${info}" MATCHES "INFO:compiler_version\\[([^]\"]*)\\]")
  796. string(REGEX REPLACE "^0+([0-9]+)" "\\1" COMPILER_VERSION "${CMAKE_MATCH_1}")
  797. string(REGEX REPLACE "\\.0+([0-9])" ".\\1" COMPILER_VERSION "${COMPILER_VERSION}")
  798. endif()
  799. if("${info}" MATCHES "INFO:compiler_version_internal\\[([^]\"]*)\\]")
  800. set(COMPILER_VERSION_INTERNAL "${CMAKE_MATCH_1}")
  801. string(REGEX REPLACE "^0+([0-9]+)" "\\1" COMPILER_VERSION_INTERNAL "${COMPILER_VERSION_INTERNAL}")
  802. string(REGEX REPLACE "\\.0+([0-9]+)" ".\\1" COMPILER_VERSION_INTERNAL "${COMPILER_VERSION_INTERNAL}")
  803. string(STRIP "${COMPILER_VERSION_INTERNAL}" COMPILER_VERSION_INTERNAL)
  804. endif()
  805. foreach(comp MAJOR MINOR PATCH TWEAK)
  806. foreach(digit 1 2 3 4 5 6 7 8 9)
  807. if("${info}" MATCHES "INFO:compiler_version_${comp}_digit_${digit}\\[([0-9])\\]")
  808. set(value ${CMAKE_MATCH_1})
  809. math(EXPR COMPILER_VERSION_${comp} "${COMPILER_VERSION_${comp}} + ${value} * ${DIGIT_VALUE_${digit}}")
  810. set(HAVE_COMPILER_VERSION_${comp} 1)
  811. endif()
  812. endforeach()
  813. endforeach()
  814. if("${info}" MATCHES "INFO:compiler_wrapper\\[([^]\"]*)\\]")
  815. set(COMPILER_WRAPPER "${CMAKE_MATCH_1}")
  816. endif()
  817. if("${info}" MATCHES "INFO:simulate\\[([^]\"]*)\\]")
  818. set(SIMULATE_ID "${CMAKE_MATCH_1}")
  819. endif()
  820. if("${info}" MATCHES "INFO:simulate_version\\[([^]\"]*)\\]")
  821. string(REGEX REPLACE "^0+([0-9])" "\\1" SIMULATE_VERSION "${CMAKE_MATCH_1}")
  822. string(REGEX REPLACE "\\.0+([0-9])" ".\\1" SIMULATE_VERSION "${SIMULATE_VERSION}")
  823. endif()
  824. if("${info}" MATCHES "INFO:qnxnto\\[\\]")
  825. set(COMPILER_QNXNTO 1)
  826. endif()
  827. if("${info}" MATCHES "INFO:dialect_default\\[([^]\"]*)\\]")
  828. set(CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT "${CMAKE_MATCH_1}")
  829. endif()
  830. endforeach()
  831. # Construct compiler version from components if needed.
  832. if(NOT DEFINED COMPILER_VERSION AND HAVE_COMPILER_VERSION_MAJOR)
  833. set(COMPILER_VERSION "${COMPILER_VERSION_MAJOR}")
  834. if(HAVE_COMPILER_VERSION_MINOR)
  835. string(APPEND COMPILER_VERSION ".${COMPILER_VERSION_MINOR}")
  836. if(HAVE_COMPILER_VERSION_PATCH)
  837. string(APPEND COMPILER_VERSION ".${COMPILER_VERSION_PATCH}")
  838. if(HAVE_COMPILER_VERSION_TWEAK)
  839. string(APPEND COMPILER_VERSION ".${COMPILER_VERSION_TWEAK}")
  840. endif()
  841. endif()
  842. endif()
  843. endif()
  844. # Detect the exact architecture from the PE header.
  845. if(WIN32)
  846. # The offset to the PE signature is stored at 0x3c.
  847. file(READ ${file} peoffsethex LIMIT 1 OFFSET 60 HEX)
  848. if(NOT peoffsethex STREQUAL "")
  849. string(SUBSTRING "${peoffsethex}" 0 1 peoffsethex1)
  850. string(SUBSTRING "${peoffsethex}" 1 1 peoffsethex2)
  851. set(peoffsetexpression "${peoffsethex1} * 16 + ${peoffsethex2}")
  852. string(REPLACE "a" "10" peoffsetexpression "${peoffsetexpression}")
  853. string(REPLACE "b" "11" peoffsetexpression "${peoffsetexpression}")
  854. string(REPLACE "c" "12" peoffsetexpression "${peoffsetexpression}")
  855. string(REPLACE "d" "13" peoffsetexpression "${peoffsetexpression}")
  856. string(REPLACE "e" "14" peoffsetexpression "${peoffsetexpression}")
  857. string(REPLACE "f" "15" peoffsetexpression "${peoffsetexpression}")
  858. math(EXPR peoffset "${peoffsetexpression}")
  859. file(READ ${file} peheader LIMIT 6 OFFSET ${peoffset} HEX)
  860. if(peheader STREQUAL "50450000a201")
  861. set(ARCHITECTURE_ID "SH3")
  862. elseif(peheader STREQUAL "50450000a301")
  863. set(ARCHITECTURE_ID "SH3DSP")
  864. elseif(peheader STREQUAL "50450000a601")
  865. set(ARCHITECTURE_ID "SH4")
  866. elseif(peheader STREQUAL "50450000a801")
  867. set(ARCHITECTURE_ID "SH5")
  868. endif()
  869. endif()
  870. endif()
  871. # Check if a valid compiler and platform were found.
  872. if(COMPILER_ID AND NOT COMPILER_ID_TWICE)
  873. set(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
  874. set(CMAKE_${lang}_PLATFORM_ID "${PLATFORM_ID}")
  875. set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
  876. set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
  877. set(CMAKE_${lang}_COMPILER_VERSION "${COMPILER_VERSION}")
  878. set(CMAKE_${lang}_COMPILER_VERSION_INTERNAL "${COMPILER_VERSION_INTERNAL}")
  879. set(CMAKE_${lang}_SIMULATE_ID "${SIMULATE_ID}")
  880. set(CMAKE_${lang}_SIMULATE_VERSION "${SIMULATE_VERSION}")
  881. endif()
  882. # Check the compiler identification string.
  883. if(CMAKE_${lang}_COMPILER_ID)
  884. # The compiler identification was found.
  885. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  886. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
  887. "${file}\"\n\n")
  888. else()
  889. # The compiler identification could not be found.
  890. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  891. "The ${lang} compiler identification could not be found in \""
  892. "${file}\"\n\n")
  893. endif()
  894. endif()
  895. # try to figure out the executable format: ELF, COFF, Mach-O
  896. if(NOT CMAKE_EXECUTABLE_FORMAT)
  897. file(READ ${file} CMAKE_EXECUTABLE_MAGIC LIMIT 4 HEX)
  898. # ELF files start with 0x7f"ELF"
  899. if("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
  900. set(CMAKE_EXECUTABLE_FORMAT "ELF" CACHE INTERNAL "Executable file format")
  901. endif()
  902. # # COFF (.exe) files start with "MZ"
  903. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
  904. # set(CMAKE_EXECUTABLE_FORMAT "COFF" CACHE INTERNAL "Executable file format")
  905. # endif()
  906. #
  907. # Mach-O files start with MH_MAGIC or MH_CIGAM
  908. if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface|cefaedfe|feedfacf|cffaedfe")
  909. set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE INTERNAL "Executable file format")
  910. endif()
  911. # XCOFF files start with 0x01 followed by 0xDF (32-bit) or 0xF7 (64-bit).
  912. if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "^01(df|f7)")
  913. set(CMAKE_EXECUTABLE_FORMAT "XCOFF" CACHE INTERNAL "Executable file format")
  914. endif()
  915. endif()
  916. if(NOT DEFINED CMAKE_EXECUTABLE_FORMAT)
  917. set(CMAKE_EXECUTABLE_FORMAT)
  918. endif()
  919. # Return the information extracted.
  920. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  921. set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  922. set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "${CMAKE_${lang}_COMPILER_ARCHITECTURE_ID}" PARENT_SCOPE)
  923. set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  924. PARENT_SCOPE)
  925. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
  926. set(CMAKE_${lang}_COMPILER_VERSION_INTERNAL "${CMAKE_${lang}_COMPILER_VERSION_INTERNAL}" PARENT_SCOPE)
  927. set(CMAKE_${lang}_COMPILER_WRAPPER "${COMPILER_WRAPPER}" PARENT_SCOPE)
  928. set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE)
  929. set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE)
  930. set(CMAKE_EXECUTABLE_FORMAT "${CMAKE_EXECUTABLE_FORMAT}" PARENT_SCOPE)
  931. set(COMPILER_QNXNTO "${COMPILER_QNXNTO}" PARENT_SCOPE)
  932. set(CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT "${CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT}" PARENT_SCOPE)
  933. endfunction()
  934. #-----------------------------------------------------------------------------
  935. # Function to query the compiler vendor.
  936. # This uses a table with entries of the form
  937. # list(APPEND CMAKE_${lang}_COMPILER_ID_VENDORS ${vendor})
  938. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor} -some-vendor-flag)
  939. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor} "Some Vendor Output")
  940. # We try running the compiler with the flag for each vendor and
  941. # matching its regular expression in the output.
  942. function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang userflags)
  943. if(NOT CMAKE_${lang}_COMPILER_ID_DIR)
  944. # We get here when this function is called not from within CMAKE_DETERMINE_COMPILER_ID()
  945. # This is done e.g. for detecting the compiler ID for assemblers.
  946. # Compute the directory in which to run the test and Create a clean working directory.
  947. set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_PLATFORM_INFO_DIR}/CompilerId${lang})
  948. file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  949. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  950. endif()
  951. # Save the current LC_ALL, LC_MESSAGES, and LANG environment variables
  952. # and set them to "C" so we get the expected output to match.
  953. set(_orig_lc_all $ENV{LC_ALL})
  954. set(_orig_lc_messages $ENV{LC_MESSAGES})
  955. set(_orig_lang $ENV{LANG})
  956. set(ENV{LC_ALL} C)
  957. set(ENV{LC_MESSAGES} C)
  958. set(ENV{LANG} C)
  959. foreach(vendor ${CMAKE_${lang}_COMPILER_ID_VENDORS})
  960. set(flags ${CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor}})
  961. set(regex ${CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor}})
  962. execute_process(
  963. COMMAND "${CMAKE_${lang}_COMPILER}"
  964. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  965. ${userflags}
  966. ${flags}
  967. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  968. OUTPUT_VARIABLE output ERROR_VARIABLE output
  969. RESULT_VARIABLE result
  970. TIMEOUT 10
  971. )
  972. if("${output}" MATCHES "${regex}")
  973. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  974. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  975. "matched \"${regex}\":\n${output}")
  976. set(CMAKE_${lang}_COMPILER_ID "${vendor}" PARENT_SCOPE)
  977. set(CMAKE_${lang}_COMPILER_ID_OUTPUT "${output}" PARENT_SCOPE)
  978. set(CMAKE_${lang}_COMPILER_ID_VENDOR_MATCH "${CMAKE_MATCH_1}" PARENT_SCOPE)
  979. break()
  980. else()
  981. if("${result}" MATCHES "timeout")
  982. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  983. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  984. "terminated after 10 s due to timeout.")
  985. else()
  986. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  987. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  988. "did not match \"${regex}\":\n${output}")
  989. endif()
  990. endif()
  991. endforeach()
  992. # Restore original LC_ALL, LC_MESSAGES, and LANG
  993. set(ENV{LC_ALL} ${_orig_lc_all})
  994. set(ENV{LC_MESSAGES} ${_orig_lc_messages})
  995. set(ENV{LANG} ${_orig_lang})
  996. endfunction()
  997. function(CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX lang userflags)
  998. # Run this MSVC-compatible compiler to detect what the /showIncludes
  999. # option displays. We can use a C source even with the C++ compiler
  1000. # because MSVC-compatible compilers handle both and show the same output.
  1001. set(showdir ${CMAKE_BINARY_DIR}/CMakeFiles/ShowIncludes)
  1002. file(WRITE ${showdir}/foo.h "\n")
  1003. file(WRITE ${showdir}/main.c "#include \"foo.h\" \nint main(){}\n")
  1004. execute_process(
  1005. COMMAND "${CMAKE_${lang}_COMPILER}"
  1006. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  1007. ${userflags}
  1008. /nologo /showIncludes /c main.c
  1009. WORKING_DIRECTORY ${showdir}
  1010. OUTPUT_VARIABLE out
  1011. ERROR_VARIABLE err
  1012. RESULT_VARIABLE res
  1013. ENCODING AUTO # cl prints in current code page
  1014. )
  1015. if(res EQUAL 0 AND "${out}" MATCHES "(^|\n)([^:\n]*:[^:\n]*:[ \t]*)")
  1016. set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "${CMAKE_MATCH_2}" PARENT_SCOPE)
  1017. else()
  1018. set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "" PARENT_SCOPE)
  1019. endif()
  1020. endfunction()