global_flags.compiler.msvc.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. if(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang
  2. AND "${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC"
  3. AND "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
  4. set(_IS_CLANG_CL_COMPILER true)
  5. else()
  6. set(_IS_CLANG_CL_COMPILER false)
  7. endif()
  8. set(_WARNS_ENABLED
  9. 4018 # 'expression' : signed/unsigned mismatch
  10. 4265 # 'class' : class has virtual functions, but destructor is not virtual
  11. 4296 # 'operator' : expression is always false
  12. 4431 # missing type specifier - int assumed
  13. )
  14. set(_WARNS_AS_ERROR
  15. 4013 # 'function' undefined; assuming extern returning int
  16. )
  17. set(_WARNS_DISABLED
  18. # While this warning corresponds to enabled-by-default -Wmacro-redefinition,
  19. # it floods clog with abundant amount of log lines,
  20. # as yvals_core.h from Windows SDK redefines certain
  21. # which macros logically belong to libcxx
  22. 4005 # '__cpp_lib_*': macro redefinition.
  23. # Ne need to recheck this, but it looks like _CRT_USE_BUILTIN_OFFSETOF still makes sense
  24. 4117 # macro name '_CRT_USE_BUILTIN_OFFSETOF' is reserved, '#define' ignored
  25. 4127 # conditional expression is constant
  26. 4200 # nonstandard extension used : zero-sized array in struct/union
  27. 4201 # nonstandard extension used : nameless struct/union
  28. 4351 # elements of array will be default initialized
  29. 4355 # 'this' : used in base member initializer list
  30. 4503 # decorated name length exceeded, name was truncated
  31. 4510 # default constructor could not be generated
  32. 4511 # copy constructor could not be generated
  33. 4512 # assignment operator could not be generated
  34. 4554 # check operator precedence for possible error; use parentheses to clarify precedence
  35. 4610 # 'object' can never be instantiated - user defined constructor required
  36. 4706 # assignment within conditional expression
  37. 4800 # forcing value to bool 'true' or 'false' (performance warning)
  38. 4996 # The POSIX name for this item is deprecated
  39. 4714 # function marked as __forceinline not inlined
  40. 4197 # 'TAtomic' : top-level volatile in cast is ignored
  41. 4245 # 'initializing' : conversion from 'int' to 'ui32', signed/unsigned mismatch
  42. 4324 # 'ystd::function<void (uint8_t *)>': structure was padded due to alignment specifier
  43. 5033 # 'register' is no longer a supported storage class
  44. )
  45. set (_MSVC_COMMON_C_CXX_FLAGS " \
  46. /DWIN32 \
  47. /D_WIN32 \
  48. /D_WINDOWS \
  49. /D_CRT_SECURE_NO_WARNINGS \
  50. /D_CRT_NONSTDC_NO_WARNINGS \
  51. /D_USE_MATH_DEFINES \
  52. /D__STDC_CONSTANT_MACROS \
  53. /D__STDC_FORMAT_MACROS \
  54. /D_USING_V110_SDK71_ \
  55. /DWIN32_LEAN_AND_MEAN \
  56. /DNOMINMAX \
  57. /nologo \
  58. /Zm500 \
  59. /GR \
  60. /bigobj \
  61. /FC \
  62. /EHs \
  63. /errorReport:prompt \
  64. /Zc:inline \
  65. /utf-8 \
  66. /permissive- \
  67. /D_WIN32_WINNT=0x0601 \
  68. /D_MBCS \
  69. ")
  70. if (NOT _IS_CLANG_CL_COMPILER)
  71. # unused by clang-cl
  72. string(APPEND _MSVC_COMMON_C_CXX_FLAGS " /MP")
  73. endif()
  74. if (CMAKE_GENERATOR MATCHES "Visual.Studio.*")
  75. string(APPEND _MSVC_COMMON_C_CXX_FLAGS "\
  76. /DY_UCRT_INCLUDE=\"$(UniversalCRT_IncludePath.Split(';')[0].Replace('\\','/'))\" \
  77. /DY_MSVC_INCLUDE=\"$(VC_VC_IncludePath.Split(';')[0].Replace('\\','/'))\" \
  78. ")
  79. else()
  80. set(UCRT_INCLUDE_FOUND false)
  81. foreach(INCLUDE_PATH $ENV{INCLUDE})
  82. if (INCLUDE_PATH MATCHES ".*\\\\Windows Kits\\\\[0-9]+\\\\include\\\\[0-9\\.]+\\\\ucrt$")
  83. message(VERBOSE "Found Y_UCRT_INCLUDE path \"${INCLUDE_PATH}\"")
  84. string(REPLACE "\\" "/" SAFE_INCLUDE_PATH "${INCLUDE_PATH}")
  85. string(APPEND _MSVC_COMMON_C_CXX_FLAGS " /DY_UCRT_INCLUDE=\"${SAFE_INCLUDE_PATH}\"")
  86. set(UCRT_INCLUDE_FOUND true)
  87. break()
  88. endif()
  89. endforeach()
  90. if (NOT UCRT_INCLUDE_FOUND)
  91. message(FATAL_ERROR "UniversalCRT include path not found, please add it to the standard INCLUDE environment variable (most likely by calling vcvars64.bat)")
  92. endif()
  93. set(MSVC_INCLUDE_FOUND false)
  94. foreach(INCLUDE_PATH $ENV{INCLUDE})
  95. if (INCLUDE_PATH MATCHES ".*VC\\\\Tools\\\\MSVC\\\\[0-9\\.]+\\\\include$")
  96. message(VERBOSE "Found Y_MSVC_INCLUDE path \"${INCLUDE_PATH}\"")
  97. string(REPLACE "\\" "/" SAFE_INCLUDE_PATH "${INCLUDE_PATH}")
  98. string(APPEND _MSVC_COMMON_C_CXX_FLAGS " /DY_MSVC_INCLUDE=\"${SAFE_INCLUDE_PATH}\"")
  99. set(MSVC_INCLUDE_FOUND true)
  100. break()
  101. endif()
  102. endforeach()
  103. if (NOT MSVC_INCLUDE_FOUND)
  104. message(FATAL_ERROR "MSVC include path not found, please add it to the standard INCLUDE environment variable (most likely by calling vcvars64.bat)")
  105. endif()
  106. endif()
  107. foreach(WARN ${_WARNS_AS_ERROR})
  108. string(APPEND _MSVC_COMMON_C_CXX_FLAGS " /we${WARN}")
  109. endforeach()
  110. foreach(WARN ${_WARNS_ENABLED})
  111. string(APPEND _MSVC_COMMON_C_CXX_FLAGS " /w1${WARN}")
  112. endforeach()
  113. foreach(WARN ${_WARNS_DISABLED})
  114. string(APPEND _MSVC_COMMON_C_CXX_FLAGS " /wd${WARN}")
  115. endforeach()
  116. if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64)$")
  117. string(APPEND _MSVC_COMMON_C_CXX_FLAGS " \
  118. /D_WIN64 \
  119. /DWIN64 \
  120. /D__SSE2__ \
  121. /D__SSE3__ \
  122. /D__SSSE3__ \
  123. /D__SSE4_1__ \
  124. /D__SSE4_2__ \
  125. /D__POPCNT__ \
  126. ")
  127. endif()
  128. if (_IS_CLANG_CL_COMPILER)
  129. # clang-cl works slighly differently than MSVC compiler when specifying arch options, so we have to set them differently
  130. # https://github.com/llvm/llvm-project/issues/56722
  131. include(global_flags.compiler.gnu.march)
  132. string(APPEND _MSVC_COMMON_C_CXX_FLAGS " ${_GNU_MARCH_C_CXX_FLAGS}")
  133. endif()
  134. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_MSVC_COMMON_C_CXX_FLAGS} \
  135. ")
  136. # TODO - '/D_CRT_USE_BUILTIN_OFFSETOF'
  137. # TODO - -DUSE_STL_SYSTEM
  138. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_MSVC_COMMON_C_CXX_FLAGS} \
  139. /std:c++latest \
  140. /Zc:__cplusplus \
  141. ")
  142. set(CMAKE_CXX_FLAGS_DEBUG "/Z7 /Ob0 /Od /D_DEBUG")
  143. set(CMAKE_CXX_FLAGS_MINSIZEREL "/O1 /Ob1 /DNDEBUG")
  144. set(CMAKE_CXX_FLAGS_RELEASE "/Ox /Ob2 /Oi /DNDEBUG")
  145. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/Z7 /Ox /Ob1 /DNDEBUG")