CMakeDetermineCompilerId.cmake 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #=============================================================================
  2. # Copyright 2007-2009 Kitware, Inc.
  3. #
  4. # Distributed under the OSI-approved BSD License (the "License");
  5. # see accompanying file Copyright.txt for details.
  6. #
  7. # This software is distributed WITHOUT ANY WARRANTY; without even the
  8. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. # See the License for more information.
  10. #=============================================================================
  11. # (To distribute this file outside of CMake, substitute the full
  12. # License text for the above reference.)
  13. # Function to compile a source file to identify the compiler. This is
  14. # used internally by CMake and should not be included by user code.
  15. # If successful, sets CMAKE_<lang>_COMPILER_ID and CMAKE_<lang>_PLATFORM_ID
  16. function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
  17. # Make sure the compiler arguments are clean.
  18. string(STRIP "${CMAKE_${lang}_COMPILER_ARG1}" CMAKE_${lang}_COMPILER_ID_ARG1)
  19. string(REGEX REPLACE " +" ";" CMAKE_${lang}_COMPILER_ID_ARG1 "${CMAKE_${lang}_COMPILER_ID_ARG1}")
  20. # Make sure user-specified compiler flags are used.
  21. if(CMAKE_${lang}_FLAGS)
  22. set(CMAKE_${lang}_COMPILER_ID_FLAGS ${CMAKE_${lang}_FLAGS})
  23. else()
  24. set(CMAKE_${lang}_COMPILER_ID_FLAGS $ENV{${flagvar}})
  25. endif()
  26. string(REGEX REPLACE " " ";" CMAKE_${lang}_COMPILER_ID_FLAGS_LIST "${CMAKE_${lang}_COMPILER_ID_FLAGS}")
  27. # Compute the directory in which to run the test.
  28. set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_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(NOT CMAKE_${lang}_COMPILER_ID)
  41. CMAKE_DETERMINE_COMPILER_ID_VENDOR(${lang})
  42. endif()
  43. # if the format is unknown after all files have been checked, put "Unknown" in the cache
  44. if(NOT CMAKE_EXECUTABLE_FORMAT)
  45. set(CMAKE_EXECUTABLE_FORMAT "Unknown" CACHE INTERNAL "Executable file format")
  46. endif()
  47. # Display the final identification result.
  48. if(CMAKE_${lang}_COMPILER_ID)
  49. if(CMAKE_${lang}_COMPILER_VERSION)
  50. set(_version " ${CMAKE_${lang}_COMPILER_VERSION}")
  51. else()
  52. set(_version "")
  53. endif()
  54. message(STATUS "The ${lang} compiler identification is "
  55. "${CMAKE_${lang}_COMPILER_ID}${_version}")
  56. else()
  57. message(STATUS "The ${lang} compiler identification is unknown")
  58. endif()
  59. # Check if compiler id detection gave us the compiler tool.
  60. if(NOT CMAKE_${lang}_COMPILER)
  61. if(CMAKE_${lang}_COMPILER_ID_TOOL)
  62. set(CMAKE_${lang}_COMPILER "${CMAKE_${lang}_COMPILER_ID_TOOL}" PARENT_SCOPE)
  63. else()
  64. set(CMAKE_${lang}_COMPILER "CMAKE_${lang}_COMPILER-NOTFOUND" PARENT_SCOPE)
  65. endif()
  66. endif()
  67. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  68. set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  69. set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  70. PARENT_SCOPE)
  71. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
  72. endfunction()
  73. #-----------------------------------------------------------------------------
  74. # Function to write the compiler id source file.
  75. function(CMAKE_DETERMINE_COMPILER_ID_WRITE lang src)
  76. file(READ ${CMAKE_ROOT}/Modules/${src}.in ID_CONTENT_IN)
  77. string(CONFIGURE "${ID_CONTENT_IN}" ID_CONTENT_OUT @ONLY)
  78. file(WRITE ${CMAKE_${lang}_COMPILER_ID_DIR}/${src} "${ID_CONTENT_OUT}")
  79. endfunction()
  80. #-----------------------------------------------------------------------------
  81. # Function to build the compiler id source file and look for output
  82. # files.
  83. function(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags src)
  84. # Create a clean working directory.
  85. file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  86. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  87. CMAKE_DETERMINE_COMPILER_ID_WRITE("${lang}" "${src}")
  88. # Construct a description of this test case.
  89. set(COMPILER_DESCRIPTION
  90. "Compiler: ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1}
  91. Build flags: ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  92. Id flags: ${testflags}
  93. ")
  94. # Compile the compiler identification source.
  95. if("${CMAKE_GENERATOR}" MATCHES "Visual Studio ([0-9]+)")
  96. set(vs_version ${CMAKE_MATCH_1})
  97. set(id_arch ${CMAKE_VS_PLATFORM_NAME})
  98. set(id_lang "${lang}")
  99. set(id_cl cl.exe)
  100. if(NOT id_arch)
  101. set(id_arch Win32)
  102. endif()
  103. if(NOT "${vs_version}" VERSION_LESS 10)
  104. set(v 10)
  105. set(ext vcxproj)
  106. elseif(NOT "${vs_version}" VERSION_LESS 7)
  107. set(id_version ${vs_version}.00)
  108. set(v 7)
  109. set(ext vcproj)
  110. else()
  111. set(v 6)
  112. set(ext dsp)
  113. endif()
  114. if("${id_arch}" STREQUAL "x64")
  115. set(id_machine_10 MachineX64)
  116. elseif("${id_arch}" STREQUAL "Itanium")
  117. set(id_machine_10 MachineIA64)
  118. set(id_arch ia64)
  119. else()
  120. set(id_machine_6 x86)
  121. set(id_machine_10 MachineX86)
  122. endif()
  123. if(CMAKE_VS_PLATFORM_TOOLSET)
  124. set(id_toolset "<PlatformToolset>${CMAKE_VS_PLATFORM_TOOLSET}</PlatformToolset>")
  125. else()
  126. set(id_toolset "")
  127. endif()
  128. if(CMAKE_VS_WINCE_VERSION)
  129. set(id_entrypoint "mainACRTStartup")
  130. set(id_subsystem 9)
  131. else()
  132. set(id_subsystem 1)
  133. endif()
  134. if("${CMAKE_MAKE_PROGRAM}" MATCHES "[Mm][Ss][Bb][Uu][Ii][Ll][Dd]")
  135. set(build /p:Configuration=Debug /p:Platform=@id_arch@ /p:VisualStudioVersion=${vs_version}.0)
  136. elseif("${CMAKE_MAKE_PROGRAM}" MATCHES "[Mm][Ss][Dd][Ee][Vv]")
  137. set(build /make)
  138. else()
  139. set(build /build Debug)
  140. endif()
  141. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  142. get_filename_component(id_src "${src}" NAME)
  143. configure_file(${CMAKE_ROOT}/Modules/CompilerId/VS-${v}.${ext}.in
  144. ${id_dir}/CompilerId${lang}.${ext} @ONLY IMMEDIATE)
  145. execute_process(
  146. COMMAND ${CMAKE_MAKE_PROGRAM} CompilerId${lang}.${ext} ${build}
  147. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  148. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  149. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  150. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  151. )
  152. # Match the compiler location line printed out.
  153. if("${CMAKE_${lang}_COMPILER_ID_OUTPUT}" MATCHES "CMAKE_${lang}_COMPILER=([^%\r\n]+)[\r\n]")
  154. set(_comp "${CMAKE_MATCH_1}")
  155. if(EXISTS "${_comp}")
  156. file(TO_CMAKE_PATH "${_comp}" _comp)
  157. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  158. endif()
  159. endif()
  160. elseif("${CMAKE_GENERATOR}" MATCHES "Xcode")
  161. set(id_lang "${lang}")
  162. set(id_type ${CMAKE_${lang}_COMPILER_XCODE_TYPE})
  163. set(id_dir ${CMAKE_${lang}_COMPILER_ID_DIR})
  164. get_filename_component(id_src "${src}" NAME)
  165. if(CMAKE_XCODE_PLATFORM_TOOLSET)
  166. set(id_toolset "GCC_VERSION = ${CMAKE_XCODE_PLATFORM_TOOLSET};")
  167. else()
  168. set(id_toolset "")
  169. endif()
  170. if(NOT ${XCODE_VERSION} VERSION_LESS 3)
  171. set(v 3)
  172. set(ext xcodeproj)
  173. elseif(NOT ${XCODE_VERSION} VERSION_LESS 2)
  174. set(v 2)
  175. set(ext xcodeproj)
  176. else()
  177. set(v 1)
  178. set(ext xcode)
  179. endif()
  180. configure_file(${CMAKE_ROOT}/Modules/CompilerId/Xcode-${v}.pbxproj.in
  181. ${id_dir}/CompilerId${lang}.${ext}/project.pbxproj @ONLY IMMEDIATE)
  182. unset(_ENV_MACOSX_DEPLOYMENT_TARGET)
  183. if(DEFINED ENV{MACOSX_DEPLOYMENT_TARGET})
  184. set(_ENV_MACOSX_DEPLOYMENT_TARGET "$ENV{MACOSX_DEPLOYMENT_TARGET}")
  185. set(ENV{MACOSX_DEPLOYMENT_TARGET} "")
  186. endif()
  187. execute_process(COMMAND xcodebuild
  188. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  189. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  190. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  191. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  192. )
  193. if(DEFINED _ENV_MACOSX_DEPLOYMENT_TARGET)
  194. set(ENV{MACOSX_DEPLOYMENT_TARGET} "${_ENV_MACOSX_DEPLOYMENT_TARGET}")
  195. endif()
  196. # Match the link line from xcodebuild output of the form
  197. # Ld ...
  198. # ...
  199. # /path/to/cc ...CompilerId${lang}/...
  200. # to extract the compiler front-end for the language.
  201. 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\\\"]")
  202. set(_comp "${CMAKE_MATCH_2}")
  203. if(EXISTS "${_comp}")
  204. set(CMAKE_${lang}_COMPILER_ID_TOOL "${_comp}" PARENT_SCOPE)
  205. endif()
  206. endif()
  207. else()
  208. if(COMMAND EXECUTE_PROCESS)
  209. execute_process(
  210. COMMAND ${CMAKE_${lang}_COMPILER}
  211. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  212. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  213. ${testflags}
  214. "${src}"
  215. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  216. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  217. ERROR_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  218. RESULT_VARIABLE CMAKE_${lang}_COMPILER_ID_RESULT
  219. )
  220. else()
  221. exec_program(
  222. ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_DIR}
  223. ARGS ${CMAKE_${lang}_COMPILER_ID_ARG1}
  224. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  225. ${testflags}
  226. \"${src}\"
  227. OUTPUT_VARIABLE CMAKE_${lang}_COMPILER_ID_OUTPUT
  228. RETURN_VALUE CMAKE_${lang}_COMPILER_ID_RESULT
  229. )
  230. endif()
  231. endif()
  232. # Check the result of compilation.
  233. if(CMAKE_${lang}_COMPILER_ID_RESULT)
  234. # Compilation failed.
  235. set(MSG
  236. "Compiling the ${lang} compiler identification source file \"${src}\" failed.
  237. ${COMPILER_DESCRIPTION}
  238. The output was:
  239. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  240. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  241. ")
  242. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log "${MSG}")
  243. #if(NOT CMAKE_${lang}_COMPILER_ID_ALLOW_FAIL)
  244. # message(FATAL_ERROR "${MSG}")
  245. #endif()
  246. # No output files should be inspected.
  247. set(COMPILER_${lang}_PRODUCED_FILES)
  248. else()
  249. # Compilation succeeded.
  250. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  251. "Compiling the ${lang} compiler identification source file \"${src}\" succeeded.
  252. ${COMPILER_DESCRIPTION}
  253. The output was:
  254. ${CMAKE_${lang}_COMPILER_ID_RESULT}
  255. ${CMAKE_${lang}_COMPILER_ID_OUTPUT}
  256. ")
  257. # Find the executable produced by the compiler, try all files in the
  258. # binary dir.
  259. file(GLOB files
  260. RELATIVE ${CMAKE_${lang}_COMPILER_ID_DIR}
  261. ${CMAKE_${lang}_COMPILER_ID_DIR}/*)
  262. list(REMOVE_ITEM files "${src}")
  263. set(COMPILER_${lang}_PRODUCED_FILES "")
  264. foreach(file ${files})
  265. if(NOT IS_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}/${file})
  266. list(APPEND COMPILER_${lang}_PRODUCED_FILES ${file})
  267. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  268. "Compilation of the ${lang} compiler identification source \""
  269. "${src}\" produced \"${file}\"\n\n")
  270. endif()
  271. endforeach()
  272. if(NOT COMPILER_${lang}_PRODUCED_FILES)
  273. # No executable was found.
  274. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  275. "Compilation of the ${lang} compiler identification source \""
  276. "${src}\" did not produce an executable in \""
  277. "${CMAKE_${lang}_COMPILER_ID_DIR}\".\n\n")
  278. endif()
  279. endif()
  280. # Return the files produced by the compilation.
  281. set(COMPILER_${lang}_PRODUCED_FILES "${COMPILER_${lang}_PRODUCED_FILES}" PARENT_SCOPE)
  282. endfunction()
  283. #-----------------------------------------------------------------------------
  284. # Function to extract the compiler id from an executable.
  285. function(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file)
  286. # Look for a compiler id if not yet known.
  287. if(NOT CMAKE_${lang}_COMPILER_ID)
  288. # Read the compiler identification string from the executable file.
  289. set(COMPILER_ID)
  290. set(COMPILER_VERSION)
  291. set(PLATFORM_ID)
  292. file(STRINGS ${file}
  293. CMAKE_${lang}_COMPILER_ID_STRINGS LIMIT_COUNT 4 REGEX "INFO:")
  294. set(HAVE_COMPILER_TWICE 0)
  295. foreach(info ${CMAKE_${lang}_COMPILER_ID_STRINGS})
  296. if("${info}" MATCHES ".*INFO:compiler\\[([^]\"]*)\\].*")
  297. if(COMPILER_ID)
  298. set(COMPILER_ID_TWICE 1)
  299. endif()
  300. string(REGEX REPLACE ".*INFO:compiler\\[([^]]*)\\].*" "\\1"
  301. COMPILER_ID "${info}")
  302. endif()
  303. if("${info}" MATCHES ".*INFO:platform\\[([^]\"]*)\\].*")
  304. string(REGEX REPLACE ".*INFO:platform\\[([^]]*)\\].*" "\\1"
  305. PLATFORM_ID "${info}")
  306. endif()
  307. if("${info}" MATCHES ".*INFO:arch\\[([^]\"]*)\\].*")
  308. string(REGEX REPLACE ".*INFO:arch\\[([^]]*)\\].*" "\\1"
  309. ARCHITECTURE_ID "${info}")
  310. endif()
  311. if("${info}" MATCHES ".*INFO:compiler_version\\[([^]\"]*)\\].*")
  312. string(REGEX REPLACE ".*INFO:compiler_version\\[([^]]*)\\].*" "\\1" COMPILER_VERSION "${info}")
  313. string(REGEX REPLACE "^0+([0-9])" "\\1" COMPILER_VERSION "${COMPILER_VERSION}")
  314. string(REGEX REPLACE "\\.0+([0-9])" ".\\1" COMPILER_VERSION "${COMPILER_VERSION}")
  315. endif()
  316. endforeach()
  317. # Check if a valid compiler and platform were found.
  318. if(COMPILER_ID AND NOT COMPILER_ID_TWICE)
  319. set(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
  320. set(CMAKE_${lang}_PLATFORM_ID "${PLATFORM_ID}")
  321. set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
  322. set(CMAKE_${lang}_COMPILER_VERSION "${COMPILER_VERSION}")
  323. endif()
  324. # Check the compiler identification string.
  325. if(CMAKE_${lang}_COMPILER_ID)
  326. # The compiler identification was found.
  327. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  328. "The ${lang} compiler identification is ${CMAKE_${lang}_COMPILER_ID}, found in \""
  329. "${file}\"\n\n")
  330. else()
  331. # The compiler identification could not be found.
  332. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  333. "The ${lang} compiler identification could not be found in \""
  334. "${file}\"\n\n")
  335. endif()
  336. endif()
  337. # try to figure out the executable format: ELF, COFF, Mach-O
  338. if(NOT CMAKE_EXECUTABLE_FORMAT)
  339. file(READ ${file} CMAKE_EXECUTABLE_MAGIC LIMIT 4 HEX)
  340. # ELF files start with 0x7f"ELF"
  341. if("${CMAKE_EXECUTABLE_MAGIC}" STREQUAL "7f454c46")
  342. set(CMAKE_EXECUTABLE_FORMAT "ELF" CACHE INTERNAL "Executable file format")
  343. endif()
  344. # # COFF (.exe) files start with "MZ"
  345. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "4d5a....")
  346. # set(CMAKE_EXECUTABLE_FORMAT "COFF" CACHE STRING "Executable file format")
  347. # endif()
  348. #
  349. # # Mach-O files start with CAFEBABE or FEEDFACE, according to http://radio.weblogs.com/0100490/2003/01/28.html
  350. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "cafebabe")
  351. # set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  352. # endif()
  353. # if("${CMAKE_EXECUTABLE_MAGIC}" MATCHES "feedface")
  354. # set(CMAKE_EXECUTABLE_FORMAT "MACHO" CACHE STRING "Executable file format")
  355. # endif()
  356. endif()
  357. if(NOT DEFINED CMAKE_EXECUTABLE_FORMAT)
  358. set(CMAKE_EXECUTABLE_FORMAT)
  359. endif()
  360. # Return the information extracted.
  361. set(CMAKE_${lang}_COMPILER_ID "${CMAKE_${lang}_COMPILER_ID}" PARENT_SCOPE)
  362. set(CMAKE_${lang}_PLATFORM_ID "${CMAKE_${lang}_PLATFORM_ID}" PARENT_SCOPE)
  363. set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
  364. PARENT_SCOPE)
  365. set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
  366. set(CMAKE_EXECUTABLE_FORMAT "${CMAKE_EXECUTABLE_FORMAT}" PARENT_SCOPE)
  367. endfunction()
  368. #-----------------------------------------------------------------------------
  369. # Function to query the compiler vendor.
  370. # This uses a table with entries of the form
  371. # list(APPEND CMAKE_${lang}_COMPILER_ID_VENDORS ${vendor})
  372. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor} -some-vendor-flag)
  373. # set(CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor} "Some Vendor Output")
  374. # We try running the compiler with the flag for each vendor and
  375. # matching its regular expression in the output.
  376. function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang)
  377. if(NOT CMAKE_${lang}_COMPILER_ID_DIR)
  378. # We get here when this function is called not from within CMAKE_DETERMINE_COMPILER_ID()
  379. # This is done e.g. for detecting the compiler ID for assemblers.
  380. # Compute the directory in which to run the test and Create a clean working directory.
  381. set(CMAKE_${lang}_COMPILER_ID_DIR ${CMAKE_PLATFORM_INFO_DIR}/CompilerId${lang})
  382. file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
  383. file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
  384. endif()
  385. foreach(vendor ${CMAKE_${lang}_COMPILER_ID_VENDORS})
  386. set(flags ${CMAKE_${lang}_COMPILER_ID_VENDOR_FLAGS_${vendor}})
  387. set(regex ${CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor}})
  388. execute_process(
  389. COMMAND ${CMAKE_${lang}_COMPILER}
  390. ${CMAKE_${lang}_COMPILER_ID_ARG1}
  391. ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}
  392. ${flags}
  393. WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
  394. OUTPUT_VARIABLE output ERROR_VARIABLE output
  395. RESULT_VARIABLE result
  396. TIMEOUT 10
  397. )
  398. if("${output}" MATCHES "${regex}")
  399. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  400. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  401. "matched \"${regex}\":\n${output}")
  402. set(CMAKE_${lang}_COMPILER_ID "${vendor}" PARENT_SCOPE)
  403. break()
  404. else()
  405. if("${result}" MATCHES "timeout")
  406. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  407. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  408. "terminated after 10 s due to timeout.")
  409. else()
  410. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  411. "Checking whether the ${lang} compiler is ${vendor} using \"${flags}\" "
  412. "did not match \"${regex}\":\n${output}")
  413. endif()
  414. endif()
  415. endforeach()
  416. endfunction()