Windows-Clang.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_CLANG)
  5. return()
  6. endif()
  7. set(__WINDOWS_CLANG 1)
  8. macro(__windows_compiler_clang_gnu lang)
  9. set(CMAKE_LIBRARY_PATH_FLAG "-L")
  10. set(CMAKE_LINK_LIBRARY_FLAG "-l")
  11. set(CMAKE_IMPORT_LIBRARY_PREFIX "")
  12. set(CMAKE_SHARED_LIBRARY_PREFIX "")
  13. set(CMAKE_SHARED_MODULE_PREFIX "")
  14. set(CMAKE_STATIC_LIBRARY_PREFIX "")
  15. set(CMAKE_EXECUTABLE_SUFFIX ".exe")
  16. set(CMAKE_IMPORT_LIBRARY_SUFFIX ".lib")
  17. set(CMAKE_SHARED_LIBRARY_SUFFIX ".dll")
  18. set(CMAKE_SHARED_MODULE_SUFFIX ".dll")
  19. set(CMAKE_STATIC_LIBRARY_SUFFIX ".lib")
  20. set(CMAKE_DEPFILE_FLAGS_${lang} "-MD -MT <OBJECT> -MF <DEPFILE>")
  21. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  22. set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll" ".dll.a" ".a" ".lib")
  23. set(CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS 1)
  24. set (CMAKE_LINK_DEF_FILE_FLAG "-Xlinker /DEF:")
  25. if("${CMAKE_${lang}_SIMULATE_VERSION}" MATCHES "^([0-9]+)\\.([0-9]+)")
  26. math(EXPR MSVC_VERSION "${CMAKE_MATCH_1}*100 + ${CMAKE_MATCH_2}")
  27. endif()
  28. # No -fPIC on Windows
  29. set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "")
  30. set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "")
  31. set(_CMAKE_${lang}_PIE_MAY_BE_SUPPORTED_BY_LINKER NO)
  32. set(CMAKE_${lang}_LINK_OPTIONS_PIE "")
  33. set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE "")
  34. set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "")
  35. set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_OBJECTS 1)
  36. set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_LIBRARIES 1)
  37. set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_INCLUDES 1)
  38. set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto")
  39. set(_CMAKE_${lang}_IPO_SUPPORTED_BY_CMAKE YES)
  40. set(_CMAKE_${lang}_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES)
  41. set(CMAKE_${lang}_ARCHIVE_CREATE_IPO "<CMAKE_AR> cr <TARGET> <LINK_FLAGS> <OBJECTS>")
  42. set(CMAKE_${lang}_ARCHIVE_APPEND_IPO "<CMAKE_AR> r <TARGET> <LINK_FLAGS> <OBJECTS>")
  43. set(CMAKE_${lang}_ARCHIVE_FINISH_IPO "<CMAKE_RANLIB> <TARGET>")
  44. # Create archiving rules to support large object file lists for static libraries.
  45. set(CMAKE_${lang}_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
  46. set(CMAKE_${lang}_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>")
  47. set(CMAKE_${lang}_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
  48. set(CMAKE_${lang}_CREATE_SHARED_LIBRARY
  49. "<CMAKE_${lang}_COMPILER> -fuse-ld=lld-link -nostartfiles -nostdlib <CMAKE_SHARED_LIBRARY_${lang}_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS> -o <TARGET> ${CMAKE_GNULD_IMAGE_VERSION} -Xlinker /implib:<TARGET_IMPLIB> -Xlinker /pdb:<TARGET_PDB> -Xlinker /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <OBJECTS> <LINK_LIBRARIES>")
  50. set(CMAKE_${lang}_CREATE_SHARED_MODULE ${CMAKE_${lang}_CREATE_SHARED_LIBRARY})
  51. set(CMAKE_${lang}_LINK_EXECUTABLE
  52. "<CMAKE_${lang}_COMPILER> -fuse-ld=lld-link -nostartfiles -nostdlib <FLAGS> <CMAKE_${lang}_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> -Xlinker /implib:<TARGET_IMPLIB> -Xlinker /pdb:<TARGET_PDB> -Xlinker /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> ${CMAKE_GNULD_IMAGE_VERSION} <LINK_LIBRARIES>")
  53. set(CMAKE_${lang}_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreaded -Xclang -flto-visibility-public-std -D_MT -Xclang --dependent-lib=libcmt)
  54. set(CMAKE_${lang}_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL -D_DLL -D_MT -Xclang --dependent-lib=msvcrt)
  55. set(CMAKE_${lang}_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug -D_DEBUG -Xclang -flto-visibility-public-std -D_MT -Xclang --dependent-lib=libcmtd)
  56. set(CMAKE_${lang}_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd)
  57. if(CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT)
  58. set(__ADDED_FLAGS "")
  59. set(__ADDED_FLAGS_DEBUG "")
  60. else()
  61. set(__ADDED_FLAGS_DEBUG "-D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd")
  62. set(__ADDED_FLAGS "-D_DLL -D_MT -Xclang --dependent-lib=msvcrt")
  63. endif()
  64. string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " -g -Xclang -gcodeview -O0 ${__ADDED_FLAGS_DEBUG}")
  65. string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " -Os -DNDEBUG ${__ADDED_FLAGS}")
  66. string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " -O3 -DNDEBUG ${__ADDED_FLAGS}")
  67. string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " -O2 -g -DNDEBUG -Xclang -gcodeview ${__ADDED_FLAGS}")
  68. set(CMAKE_INCLUDE_SYSTEM_FLAG_${lang} "-isystem ")
  69. set(CMAKE_PCH_EXTENSION .pch)
  70. set(CMAKE_PCH_PROLOGUE "#pragma clang system_header")
  71. set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -Xclang -include-pch -Xclang <PCH_FILE>)
  72. set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -Xclang -emit-pch -Xclang -include -Xclang <PCH_HEADER>)
  73. unset(__ADDED_FLAGS)
  74. unset(__ADDED_FLAGS_DEBUG)
  75. string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE_LOWER)
  76. set(CMAKE_${lang}_STANDARD_LIBRARIES_INIT "-lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames")
  77. enable_language(RC)
  78. endmacro()
  79. if("x${CMAKE_C_SIMULATE_ID}" STREQUAL "xMSVC"
  80. OR "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC")
  81. if ( DEFINED CMAKE_C_COMPILER_ID AND DEFINED CMAKE_CXX_COMPILER_ID
  82. AND NOT "x${CMAKE_C_COMPILER_ID}" STREQUAL "x${CMAKE_CXX_COMPILER_ID}")
  83. message(FATAL_ERROR "The current configuration mixes Clang and MSVC or "
  84. "some other CL compatible compiler tool. This is not supported. "
  85. "Use either clang or MSVC as both C and C++ compilers.")
  86. endif()
  87. if ( DEFINED CMAKE_C_COMPILER_FRONTEND_VARIANT AND DEFINED CMAKE_CXX_COMPILER_FRONTEND_VARIANT
  88. AND NOT "x${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "x${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}")
  89. message(FATAL_ERROR "The current configuration uses the Clang compiler "
  90. "tool with mixed frontend variants, both the GNU and in MSVC CL "
  91. "like variants. This is not supported. Use either clang/clang++ "
  92. "or clang-cl as both C and C++ compilers.")
  93. endif()
  94. if(NOT CMAKE_RC_COMPILER_INIT)
  95. # Check if rc is already in the path
  96. # This may happen in cases where the user is already in a visual studio environment when CMake is invoked
  97. find_program(__RC_COMPILER_PATH NAMES rc)
  98. # Default to rc if it's available, otherwise fall back to llvm-rc
  99. if(__RC_COMPILER_PATH)
  100. set(CMAKE_RC_COMPILER_INIT rc)
  101. else()
  102. set(CMAKE_RC_COMPILER_INIT llvm-rc)
  103. endif()
  104. unset(__RC_COMPILER_PATH CACHE)
  105. endif()
  106. if ( "x${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "xMSVC" OR "x${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "xMSVC" )
  107. include(Platform/Windows-MSVC)
  108. macro(__windows_compiler_clang lang)
  109. set(_COMPILE_${lang} "${_COMPILE_${lang}_MSVC}")
  110. __windows_compiler_msvc(${lang})
  111. endmacro()
  112. else()
  113. cmake_policy(GET CMP0091 __WINDOWS_CLANG_CMP0091)
  114. if(__WINDOWS_CLANG_CMP0091 STREQUAL "NEW")
  115. set(CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
  116. else()
  117. set(CMAKE_MSVC_RUNTIME_LIBRARY_DEFAULT "")
  118. endif()
  119. unset(__WINDOWS_CLANG_CMP0091)
  120. set(CMAKE_BUILD_TYPE_INIT Debug)
  121. macro(__windows_compiler_clang lang)
  122. __windows_compiler_clang_gnu(${lang})
  123. endmacro()
  124. endif()
  125. else()
  126. include(Platform/Windows-GNU)
  127. macro(__windows_compiler_clang lang)
  128. __windows_compiler_gnu(${lang})
  129. endmacro()
  130. endif()