Windows-MSVC.cmake 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This module is shared by multiple languages; use include blocker.
  4. if(__WINDOWS_MSVC)
  5. return()
  6. endif()
  7. set(__WINDOWS_MSVC 1)
  8. set(CMAKE_LIBRARY_PATH_FLAG "-LIBPATH:")
  9. set(CMAKE_LINK_LIBRARY_FLAG "")
  10. set(MSVC 1)
  11. # hack: if a new cmake (which uses CMAKE_LINKER) runs on an old build tree
  12. # (where link was hardcoded) and where CMAKE_LINKER isn't in the cache
  13. # and still cmake didn't fail in CMakeFindBinUtils.cmake (because it isn't rerun)
  14. # hardcode CMAKE_LINKER here to link, so it behaves as it did before, Alex
  15. if(NOT DEFINED CMAKE_LINKER)
  16. set(CMAKE_LINKER link)
  17. endif()
  18. if(CMAKE_VERBOSE_MAKEFILE)
  19. set(CMAKE_CL_NOLOGO)
  20. else()
  21. set(CMAKE_CL_NOLOGO "/nologo")
  22. endif()
  23. if(CMAKE_SYSTEM_NAME STREQUAL "WindowsCE")
  24. set(CMAKE_CREATE_WIN32_EXE "/entry:WinMainCRTStartup")
  25. set(CMAKE_CREATE_CONSOLE_EXE "/entry:mainACRTStartup")
  26. set(_PLATFORM_LINK_FLAGS " /subsystem:windowsce")
  27. else()
  28. set(CMAKE_CREATE_WIN32_EXE "/subsystem:windows")
  29. set(CMAKE_CREATE_CONSOLE_EXE "/subsystem:console")
  30. set(_PLATFORM_LINK_FLAGS "")
  31. endif()
  32. set(CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS 1)
  33. if(NOT CMAKE_NO_BUILD_TYPE AND CMAKE_GENERATOR MATCHES "Visual Studio")
  34. set (CMAKE_NO_BUILD_TYPE 1)
  35. endif()
  36. if("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
  37. set(MSVC_IDE 1)
  38. else()
  39. set(MSVC_IDE 0)
  40. endif()
  41. if(NOT MSVC_VERSION)
  42. if("x${CMAKE_C_COMPILER_ID}" STREQUAL "xMSVC")
  43. set(_compiler_version ${CMAKE_C_COMPILER_VERSION})
  44. elseif("x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC")
  45. set(_compiler_version ${CMAKE_CXX_COMPILER_VERSION})
  46. elseif(CMAKE_C_SIMULATE_VERSION)
  47. set(_compiler_version ${CMAKE_C_SIMULATE_VERSION})
  48. elseif(CMAKE_CXX_SIMULATE_VERSION)
  49. set(_compiler_version ${CMAKE_CXX_SIMULATE_VERSION})
  50. elseif(CMAKE_Fortran_SIMULATE_VERSION)
  51. set(_compiler_version ${CMAKE_Fortran_SIMULATE_VERSION})
  52. elseif(CMAKE_CUDA_SIMULATE_VERSION)
  53. set(_compiler_version ${CMAKE_CUDA_SIMULATE_VERSION})
  54. elseif(CMAKE_C_COMPILER_VERSION)
  55. set(_compiler_version ${CMAKE_C_COMPILER_VERSION})
  56. else()
  57. set(_compiler_version ${CMAKE_CXX_COMPILER_VERSION})
  58. endif()
  59. if("${_compiler_version}" MATCHES "^([0-9]+)\\.([0-9]+)")
  60. math(EXPR MSVC_VERSION "${CMAKE_MATCH_1}*100 + ${CMAKE_MATCH_2}")
  61. else()
  62. message(FATAL_ERROR "MSVC compiler version not detected properly: ${_compiler_version}")
  63. endif()
  64. if(MSVC_VERSION GREATER_EQUAL 1920)
  65. # VS 2019 or greater
  66. set(MSVC_TOOLSET_VERSION 142)
  67. elseif(MSVC_VERSION GREATER_EQUAL 1910)
  68. # VS 2017 or greater
  69. set(MSVC_TOOLSET_VERSION 141)
  70. elseif(MSVC_VERSION EQUAL 1900)
  71. # VS 2015
  72. set(MSVC_TOOLSET_VERSION 140)
  73. elseif(MSVC_VERSION EQUAL 1800)
  74. # VS 2013
  75. set(MSVC_TOOLSET_VERSION 120)
  76. elseif(MSVC_VERSION EQUAL 1700)
  77. # VS 2012
  78. set(MSVC_TOOLSET_VERSION 110)
  79. elseif(MSVC_VERSION EQUAL 1600)
  80. # VS 2010
  81. set(MSVC_TOOLSET_VERSION 100)
  82. elseif(MSVC_VERSION EQUAL 1500)
  83. # VS 2008
  84. set(MSVC_TOOLSET_VERSION 90)
  85. elseif(MSVC_VERSION EQUAL 1400)
  86. # VS 2005
  87. set(MSVC_TOOLSET_VERSION 80)
  88. else()
  89. # We don't support MSVC_TOOLSET_VERSION for earlier compiler.
  90. endif()
  91. set(MSVC10)
  92. set(MSVC11)
  93. set(MSVC12)
  94. set(MSVC14)
  95. set(MSVC60)
  96. set(MSVC70)
  97. set(MSVC71)
  98. set(MSVC80)
  99. set(MSVC90)
  100. set(CMAKE_COMPILER_2005)
  101. set(CMAKE_COMPILER_SUPPORTS_PDBTYPE)
  102. if(NOT "${_compiler_version}" VERSION_LESS 20)
  103. # We no longer provide per-version variables. Use MSVC_VERSION instead.
  104. elseif(NOT "${_compiler_version}" VERSION_LESS 19)
  105. set(MSVC14 1)
  106. elseif(NOT "${_compiler_version}" VERSION_LESS 18)
  107. set(MSVC12 1)
  108. elseif(NOT "${_compiler_version}" VERSION_LESS 17)
  109. set(MSVC11 1)
  110. elseif(NOT "${_compiler_version}" VERSION_LESS 16)
  111. set(MSVC10 1)
  112. elseif(NOT "${_compiler_version}" VERSION_LESS 15)
  113. set(MSVC90 1)
  114. elseif(NOT "${_compiler_version}" VERSION_LESS 14)
  115. set(MSVC80 1)
  116. set(CMAKE_COMPILER_2005 1)
  117. elseif(NOT "${_compiler_version}" VERSION_LESS 13.10)
  118. set(MSVC71 1)
  119. elseif(NOT "${_compiler_version}" VERSION_LESS 13)
  120. set(MSVC70 1)
  121. else()
  122. set(MSVC60 1)
  123. set(CMAKE_COMPILER_SUPPORTS_PDBTYPE 1)
  124. endif()
  125. endif()
  126. if(MSVC_C_ARCHITECTURE_ID MATCHES 64 OR MSVC_CXX_ARCHITECTURE_ID MATCHES 64)
  127. set(CMAKE_CL_64 1)
  128. else()
  129. set(CMAKE_CL_64 0)
  130. endif()
  131. if(CMAKE_FORCE_WIN64 OR CMAKE_FORCE_IA64)
  132. set(CMAKE_CL_64 1)
  133. endif()
  134. if("${MSVC_VERSION}" GREATER 1599)
  135. set(MSVC_INCREMENTAL_DEFAULT ON)
  136. endif()
  137. # default to Debug builds
  138. set(CMAKE_BUILD_TYPE_INIT Debug)
  139. # Compute an architecture family from the architecture id.
  140. foreach(lang C CXX)
  141. set(_MSVC_${lang}_ARCHITECTURE_FAMILY "${MSVC_${lang}_ARCHITECTURE_ID}")
  142. if(_MSVC_${lang}_ARCHITECTURE_FAMILY MATCHES "^ARM64")
  143. set(_MSVC_${lang}_ARCHITECTURE_FAMILY "ARM64")
  144. elseif(_MSVC_${lang}_ARCHITECTURE_FAMILY MATCHES "^ARM")
  145. set(_MSVC_${lang}_ARCHITECTURE_FAMILY "ARM")
  146. elseif(_MSVC_${lang}_ARCHITECTURE_FAMILY MATCHES "^SH")
  147. set(_MSVC_${lang}_ARCHITECTURE_FAMILY "SHx")
  148. endif()
  149. endforeach()
  150. if(WINCE)
  151. foreach(lang C CXX)
  152. string(TOUPPER "${_MSVC_${lang}_ARCHITECTURE_FAMILY}" _MSVC_${lang}_ARCHITECTURE_FAMILY_UPPER)
  153. endforeach()
  154. if("${CMAKE_SYSTEM_VERSION}" MATCHES "^([0-9]+)\\.([0-9]+)")
  155. math(EXPR _CE_VERSION "${CMAKE_MATCH_1}*100 + ${CMAKE_MATCH_2}")
  156. elseif("${CMAKE_SYSTEM_VERSION}" STREQUAL "")
  157. set(_CE_VERSION "500")
  158. else()
  159. message(FATAL_ERROR "Invalid Windows CE version: ${CMAKE_SYSTEM_VERSION}")
  160. endif()
  161. set(_PLATFORM_DEFINES "/D_WIN32_WCE=0x${_CE_VERSION} /DUNDER_CE /DWINCE")
  162. set(_PLATFORM_DEFINES_C " /D${_MSVC_C_ARCHITECTURE_FAMILY} /D_${_MSVC_C_ARCHITECTURE_FAMILY_UPPER}_")
  163. set(_PLATFORM_DEFINES_CXX " /D${_MSVC_CXX_ARCHITECTURE_FAMILY} /D_${_MSVC_CXX_ARCHITECTURE_FAMILY_UPPER}_")
  164. set(_RTC1 "")
  165. set(_FLAGS_C "")
  166. set(_FLAGS_CXX " /GR /EHsc")
  167. foreach(lang C CXX)
  168. if(_MSVC_${lang}_ARCHITECTURE_FAMILY STREQUAL "ARM")
  169. string(APPEND _PLATFORM_DEFINES_${lang} " /D${MSVC_${lang}_ARCHITECTURE_ID}")
  170. if(MSVC_${lang}_ARCHITECTURE_ID MATCHES "^ARMV([45])I$")
  171. string(APPEND _FLAGS_${lang} " /QRarch${CMAKE_MATCH_1}T")
  172. endif()
  173. endif()
  174. endforeach()
  175. set(CMAKE_C_STANDARD_LIBRARIES_INIT "coredll.lib ole32.lib oleaut32.lib uuid.lib commctrl.lib")
  176. foreach(t EXE SHARED MODULE)
  177. string(APPEND CMAKE_${t}_LINKER_FLAGS_INIT " /NODEFAULTLIB:libc.lib /NODEFAULTLIB:oldnames.lib")
  178. endforeach()
  179. if (MSVC_VERSION LESS 1600)
  180. string(APPEND CMAKE_C_STANDARD_LIBRARIES_INIT " corelibc.lib")
  181. endif ()
  182. elseif(WINDOWS_PHONE OR WINDOWS_STORE)
  183. set(_PLATFORM_DEFINES "/DWIN32")
  184. set(_FLAGS_C " /DUNICODE /D_UNICODE")
  185. set(_FLAGS_CXX " /DUNICODE /D_UNICODE /GR /EHsc")
  186. if(WINDOWS_STORE AND MSVC_VERSION GREATER 1899)
  187. set(CMAKE_C_STANDARD_LIBRARIES_INIT "WindowsApp.lib")
  188. elseif(WINDOWS_PHONE)
  189. set(CMAKE_C_STANDARD_LIBRARIES_INIT "WindowsPhoneCore.lib RuntimeObject.lib PhoneAppModelHost.lib")
  190. elseif(_MSVC_C_ARCHITECTURE_FAMILY STREQUAL "ARM" OR _MSVC_CXX_ARCHITECTURE_FAMILY STREQUAL "ARM" OR _MSVC_C_ARCHITECTURE_FAMILY STREQUAL "ARM64" OR _MSVC_CXX_ARCHITECTURE_FAMILY STREQUAL "ARM64")
  191. set(CMAKE_C_STANDARD_LIBRARIES_INIT "kernel32.lib user32.lib")
  192. else()
  193. set(CMAKE_C_STANDARD_LIBRARIES_INIT "kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib")
  194. endif()
  195. else()
  196. set(_PLATFORM_DEFINES "/DWIN32")
  197. if(_MSVC_C_ARCHITECTURE_FAMILY STREQUAL "ARM" OR _MSVC_CXX_ARCHITECTURE_FAMILY STREQUAL "ARM" OR _MSVC_C_ARCHITECTURE_FAMILY STREQUAL "ARM64" OR _MSVC_CXX_ARCHITECTURE_FAMILY STREQUAL "ARM64")
  198. set(CMAKE_C_STANDARD_LIBRARIES_INIT "kernel32.lib user32.lib")
  199. elseif(MSVC_VERSION GREATER 1310)
  200. if(CMAKE_VS_PLATFORM_TOOLSET MATCHES "v[0-9]+_clang_.*")
  201. # Clang/C2 in MSVC14 Update 1 seems to not support -fsantinize (yet?)
  202. # set(_RTC1 "-fsantinize=memory,safe-stack")
  203. set(_FLAGS_CXX " -frtti -fexceptions")
  204. else()
  205. set(_RTC1 "/RTC1")
  206. set(_FLAGS_CXX " /GR /EHsc")
  207. endif()
  208. set(CMAKE_C_STANDARD_LIBRARIES_INIT "kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib")
  209. else()
  210. set(_RTC1 "/GZ")
  211. set(_FLAGS_CXX " /GR /GX")
  212. set(CMAKE_C_STANDARD_LIBRARIES_INIT "kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib")
  213. endif()
  214. if(MSVC_VERSION LESS 1310)
  215. set(_FLAGS_C " /Zm1000${_FLAGS_C}")
  216. set(_FLAGS_CXX " /Zm1000${_FLAGS_CXX}")
  217. endif()
  218. endif()
  219. set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "${CMAKE_C_STANDARD_LIBRARIES_INIT}")
  220. # executable linker flags
  221. set (CMAKE_LINK_DEF_FILE_FLAG "/DEF:")
  222. # set the machine type
  223. if(MSVC_C_ARCHITECTURE_ID)
  224. if(MSVC_C_ARCHITECTURE_ID MATCHES "^ARMV.I")
  225. set(_MACHINE_ARCH_FLAG "/machine:THUMB")
  226. elseif(_MSVC_C_ARCHITECTURE_FAMILY STREQUAL "ARM64")
  227. set(_MACHINE_ARCH_FLAG "/machine:ARM64")
  228. elseif(_MSVC_C_ARCHITECTURE_FAMILY STREQUAL "ARM")
  229. set(_MACHINE_ARCH_FLAG "/machine:ARM")
  230. else()
  231. set(_MACHINE_ARCH_FLAG "/machine:${MSVC_C_ARCHITECTURE_ID}")
  232. endif()
  233. elseif(MSVC_CXX_ARCHITECTURE_ID)
  234. if(MSVC_CXX_ARCHITECTURE_ID MATCHES "^ARMV.I")
  235. set(_MACHINE_ARCH_FLAG "/machine:THUMB")
  236. elseif(_MSVC_CXX_ARCHITECTURE_FAMILY STREQUAL "ARM64")
  237. set(_MACHINE_ARCH_FLAG "/machine:ARM64")
  238. elseif(_MSVC_CXX_ARCHITECTURE_FAMILY STREQUAL "ARM")
  239. set(_MACHINE_ARCH_FLAG "/machine:ARM")
  240. else()
  241. set(_MACHINE_ARCH_FLAG "/machine:${MSVC_CXX_ARCHITECTURE_ID}")
  242. endif()
  243. elseif(MSVC_Fortran_ARCHITECTURE_ID)
  244. set(_MACHINE_ARCH_FLAG "/machine:${MSVC_Fortran_ARCHITECTURE_ID}")
  245. endif()
  246. # add /debug and /INCREMENTAL:YES to DEBUG and RELWITHDEBINFO also add pdbtype
  247. # on versions that support it
  248. set( MSVC_INCREMENTAL_YES_FLAG "")
  249. if(NOT WINDOWS_PHONE AND NOT WINDOWS_STORE)
  250. if(NOT MSVC_INCREMENTAL_DEFAULT)
  251. set( MSVC_INCREMENTAL_YES_FLAG "/INCREMENTAL:YES")
  252. else()
  253. set( MSVC_INCREMENTAL_YES_FLAG "/INCREMENTAL" )
  254. endif()
  255. endif()
  256. foreach(t EXE SHARED MODULE)
  257. string(APPEND CMAKE_${t}_LINKER_FLAGS_INIT " ${_MACHINE_ARCH_FLAG}")
  258. if (CMAKE_COMPILER_SUPPORTS_PDBTYPE)
  259. string(APPEND CMAKE_${t}_LINKER_FLAGS_DEBUG_INIT " /debug /pdbtype:sept ${MSVC_INCREMENTAL_YES_FLAG}")
  260. string(APPEND CMAKE_${t}_LINKER_FLAGS_RELWITHDEBINFO_INIT " /debug /pdbtype:sept ${MSVC_INCREMENTAL_YES_FLAG}")
  261. else ()
  262. string(APPEND CMAKE_${t}_LINKER_FLAGS_DEBUG_INIT " /debug ${MSVC_INCREMENTAL_YES_FLAG}")
  263. string(APPEND CMAKE_${t}_LINKER_FLAGS_RELWITHDEBINFO_INIT " /debug ${MSVC_INCREMENTAL_YES_FLAG}")
  264. endif ()
  265. # for release and minsize release default to no incremental linking
  266. string(APPEND CMAKE_${t}_LINKER_FLAGS_MINSIZEREL_INIT " /INCREMENTAL:NO")
  267. string(APPEND CMAKE_${t}_LINKER_FLAGS_RELEASE_INIT " /INCREMENTAL:NO")
  268. endforeach()
  269. string(APPEND CMAKE_STATIC_LINKER_FLAGS_INIT " ${_MACHINE_ARCH_FLAG}")
  270. unset(_MACHINE_ARCH_FLAG)
  271. cmake_policy(GET CMP0091 __WINDOWS_MSVC_CMP0091)
  272. if(__WINDOWS_MSVC_CMP0091 STREQUAL "NEW")
  273. set(CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
  274. else()
  275. set(CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT "")
  276. endif()
  277. unset(__WINDOWS_MSVC_CMP0091)
  278. macro(__windows_compiler_msvc lang)
  279. if(NOT MSVC_VERSION LESS 1400)
  280. # for 2005 make sure the manifest is put in the dll with mt
  281. set(_CMAKE_VS_LINK_DLL "<CMAKE_COMMAND> -E vs_link_dll --intdir=<OBJECT_DIR> --rc=<CMAKE_RC_COMPILER> --mt=<CMAKE_MT> --manifests <MANIFESTS> -- ")
  282. set(_CMAKE_VS_LINK_EXE "<CMAKE_COMMAND> -E vs_link_exe --intdir=<OBJECT_DIR> --rc=<CMAKE_RC_COMPILER> --mt=<CMAKE_MT> --manifests <MANIFESTS> -- ")
  283. endif()
  284. set(CMAKE_${lang}_CREATE_SHARED_LIBRARY
  285. "${_CMAKE_VS_LINK_DLL}<CMAKE_LINKER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} /out:<TARGET> /implib:<TARGET_IMPLIB> /pdb:<TARGET_PDB> /dll /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR>${_PLATFORM_LINK_FLAGS} <LINK_FLAGS> <LINK_LIBRARIES> ${CMAKE_END_TEMP_FILE}")
  286. set(CMAKE_${lang}_CREATE_SHARED_MODULE ${CMAKE_${lang}_CREATE_SHARED_LIBRARY})
  287. set(CMAKE_${lang}_CREATE_STATIC_LIBRARY "<CMAKE_LINKER> /lib ${CMAKE_CL_NOLOGO} <LINK_FLAGS> /out:<TARGET> <OBJECTS> ")
  288. set(CMAKE_${lang}_COMPILE_OBJECT
  289. "<CMAKE_${lang}_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO}${_COMPILE_${lang}} <DEFINES> <INCLUDES> <FLAGS> /Fo<OBJECT> /Fd<TARGET_COMPILE_PDB>${_FS_${lang}} -c <SOURCE>${CMAKE_END_TEMP_FILE}")
  290. set(CMAKE_${lang}_CREATE_PREPROCESSED_SOURCE
  291. "<CMAKE_${lang}_COMPILER> > <PREPROCESSED_SOURCE> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO}${_COMPILE_${lang}} <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE>${CMAKE_END_TEMP_FILE}")
  292. set(CMAKE_${lang}_CREATE_ASSEMBLY_SOURCE
  293. "<CMAKE_${lang}_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO}${_COMPILE_${lang}} <DEFINES> <INCLUDES> <FLAGS> /FoNUL /FAs /Fa<ASSEMBLY_SOURCE> /c <SOURCE>${CMAKE_END_TEMP_FILE}")
  294. set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_OBJECTS 1)
  295. set(CMAKE_${lang}_LINK_EXECUTABLE
  296. "${_CMAKE_VS_LINK_EXE}<CMAKE_LINKER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} /out:<TARGET> /implib:<TARGET_IMPLIB> /pdb:<TARGET_PDB> /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR>${_PLATFORM_LINK_FLAGS} <CMAKE_${lang}_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>${CMAKE_END_TEMP_FILE}")
  297. set(CMAKE_PCH_EXTENSION .pch)
  298. set(CMAKE_LINK_PCH ON)
  299. if(MSVC_VERSION GREATER_EQUAL 1910)
  300. # VS 2017 or greater
  301. if (NOT ${CMAKE_${lang}_COMPILER_ID} STREQUAL "Clang")
  302. set(CMAKE_PCH_PROLOGUE "#pragma system_header")
  303. else()
  304. set(CMAKE_PCH_PROLOGUE "#pragma clang system_header")
  305. endif()
  306. endif()
  307. if (NOT ${CMAKE_${lang}_COMPILER_ID} STREQUAL "Clang")
  308. set(CMAKE_PCH_COPY_COMPILE_PDB ON)
  309. endif()
  310. set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH /Yu<PCH_HEADER> /Fp<PCH_FILE> /FI<PCH_HEADER>)
  311. set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH /Yc<PCH_HEADER> /Fp<PCH_FILE> /FI<PCH_HEADER>)
  312. if("x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xMSVC")
  313. set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
  314. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
  315. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "/GL")
  316. set(CMAKE_${lang}_LINK_OPTIONS_IPO "/INCREMENTAL:NO" "/LTCG")
  317. string(REPLACE "<LINK_FLAGS> " "/LTCG <LINK_FLAGS> "
  318. CMAKE_${lang}_CREATE_STATIC_LIBRARY_IPO "${CMAKE_${lang}_CREATE_STATIC_LIBRARY}")
  319. elseif("x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xClang" OR
  320. "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xFlang")
  321. set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
  322. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
  323. # '-flto=thin' available since Clang 3.9 and Xcode 8
  324. # * http://clang.llvm.org/docs/ThinLTO.html#clang-llvm
  325. # * https://trac.macports.org/wiki/XcodeVersionInfo
  326. set(_CMAKE_LTO_THIN TRUE)
  327. if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.9)
  328. set(_CMAKE_LTO_THIN FALSE)
  329. endif()
  330. if(_CMAKE_LTO_THIN)
  331. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto=thin")
  332. else()
  333. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto")
  334. endif()
  335. endif()
  336. if("x${lang}" STREQUAL "xC" OR
  337. "x${lang}" STREQUAL "xCXX")
  338. if(CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT)
  339. set(_MDd "")
  340. set(_MD "")
  341. else()
  342. set(_MDd " /MDd")
  343. set(_MD " /MD")
  344. endif()
  345. cmake_policy(GET CMP0092 _cmp0092)
  346. if(_cmp0092 STREQUAL "NEW")
  347. set(_W3 "")
  348. set(_Wall "")
  349. else()
  350. set(_W3 " /W3")
  351. set(_Wall " -Wall")
  352. endif()
  353. unset(_cmp0092)
  354. if(CMAKE_VS_PLATFORM_TOOLSET MATCHES "v[0-9]+_clang_.*")
  355. # note: MSVC 14 2015 Update 1 sets -fno-ms-compatibility by default, but this does not allow one to compile many projects
  356. # that include MS's own headers. CMake itself is affected project too.
  357. string(APPEND CMAKE_${lang}_FLAGS_INIT " ${_PLATFORM_DEFINES}${_PLATFORM_DEFINES_${lang}} -fms-extensions -fms-compatibility -D_WINDOWS${_Wall}${_FLAGS_${lang}}")
  358. string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT "${_MDd} -gline-tables-only -fno-inline -O0 ${_RTC1}")
  359. string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT "${_MD} -O2 -DNDEBUG")
  360. string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT "${_MD} -gline-tables-only -O2 -fno-inline -DNDEBUG")
  361. string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT "${_MD} -DNDEBUG") # TODO: Add '-Os' once VS generator maps it properly for Clang
  362. else()
  363. string(APPEND CMAKE_${lang}_FLAGS_INIT " ${_PLATFORM_DEFINES}${_PLATFORM_DEFINES_${lang}} /D_WINDOWS${_W3}${_FLAGS_${lang}}")
  364. string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT "${_MDd} /Zi /Ob0 /Od ${_RTC1}")
  365. string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT "${_MD} /O2 /Ob2 /DNDEBUG")
  366. string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT "${_MD} /Zi /O2 /Ob1 /DNDEBUG")
  367. string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT "${_MD} /O1 /Ob1 /DNDEBUG")
  368. endif()
  369. unset(_Wall)
  370. unset(_W3)
  371. unset(_MDd)
  372. unset(_MD)
  373. set(CMAKE_${lang}_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreaded -MT)
  374. set(CMAKE_${lang}_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL -MD)
  375. set(CMAKE_${lang}_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug -MTd)
  376. set(CMAKE_${lang}_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL -MDd)
  377. endif()
  378. set(CMAKE_${lang}_LINKER_SUPPORTS_PDB ON)
  379. set(CMAKE_NINJA_DEPTYPE_${lang} msvc)
  380. __windows_compiler_msvc_enable_rc("${_PLATFORM_DEFINES} ${_PLATFORM_DEFINES_${lang}}")
  381. endmacro()
  382. macro(__windows_compiler_msvc_enable_rc flags)
  383. if(NOT CMAKE_RC_COMPILER_INIT)
  384. set(CMAKE_RC_COMPILER_INIT rc)
  385. endif()
  386. if(NOT CMAKE_RC_FLAGS_INIT)
  387. # llvm-rc fails when flags are specified with /D and no space after
  388. string(REPLACE " /D" " -D" fixed_flags " ${flags}")
  389. string(APPEND CMAKE_RC_FLAGS_INIT " ${fixed_flags}")
  390. endif()
  391. if(NOT CMAKE_RC_FLAGS_DEBUG_INIT)
  392. string(APPEND CMAKE_RC_FLAGS_DEBUG_INIT " -D_DEBUG")
  393. endif()
  394. enable_language(RC)
  395. if(NOT DEFINED CMAKE_NINJA_CMCLDEPS_RC)
  396. set(CMAKE_NINJA_CMCLDEPS_RC 1)
  397. endif()
  398. endmacro()