Configurations_msvc.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. get_property(O3DE_SCRIPT_ONLY GLOBAL PROPERTY "O3DE_SCRIPT_ONLY")
  9. # Exceptions are disabled by default. Use this to turn them on just for a specific target.
  10. set(O3DE_COMPILE_OPTION_ENABLE_EXCEPTIONS PUBLIC /EHsc)
  11. # O3DE Sets visibility to hidden by default, requiring explicit export on non-windows platforms
  12. # But on MSVC or MS-Clang, these compilers use MSVC compiler options and behavior, which means
  13. # it is not necessary to set visibility to hidden as on MSVC, things behave similar to if
  14. # hidden by default. As such, there is no need to change compile options for 3rd Party Libraries
  15. # to cause them to export symbols. This is thus blank
  16. set(O3DE_COMPILE_OPTION_EXPORT_SYMBOLS "")
  17. # By default, O3DE sets warning level 4 and sets warnings as errors. If you're pulling in
  18. # external code (from 3rd Party libraries) you can't really control whether they generate
  19. # warnings or not, and its usually out of scope to fix them. Add this compile option to
  20. # those 3rd Party targets ONLY.
  21. set(O3DE_COMPILE_OPTION_DISABLE_WARNINGS PRIVATE /W0)
  22. if (NOT O3DE_SCRIPT_ONLY)
  23. set(minimum_supported_toolset 142)
  24. if(MSVC_TOOLSET_VERSION VERSION_LESS ${minimum_supported_toolset})
  25. message(FATAL_ERROR "MSVC toolset ${MSVC_TOOLSET_VERSION} is too old, minimum supported toolset is ${minimum_supported_toolset}")
  26. endif()
  27. unset(minimum_supported_toolset)
  28. endif()
  29. include(cmake/Platform/Common/Configurations_common.cmake)
  30. include(cmake/Platform/Common/MSVC/VisualStudio_common.cmake)
  31. include(cmake/Platform/Common/MSVC/CompilerCache_msvc.cmake)
  32. # Verify that it wasn't invoked with an unsupported target/host architecture. Currently only supports x64/x64
  33. if(CMAKE_VS_PLATFORM_NAME AND NOT CMAKE_VS_PLATFORM_NAME STREQUAL "x64")
  34. message(FATAL_ERROR "${CMAKE_VS_PLATFORM_NAME} target architecture is not supported, it must be 'x64'")
  35. endif()
  36. if(CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE AND NOT CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE STREQUAL "x64")
  37. message(FATAL_ERROR "${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE} host toolset is not supported, it must be 'x64'")
  38. endif()
  39. ly_append_configurations_options(
  40. DEFINES
  41. _ENABLE_EXTENDED_ALIGNED_STORAGE # Enables support for extended alignment for the MSVC std::aligned_storage class
  42. _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING # Prevents triggering of STL4043 when checked iterators are used in 3rdParty libraries(QT and AWSNativeSDK)
  43. COMPILATION
  44. /fp:fast # allows the compiler to reorder, combine, or simplify floating-point operations to optimize floating-point code for speed and space
  45. /Gd # Use _cdecl calling convention for all functions
  46. /MP # Multicore compilation in Visual Studio
  47. /nologo # Suppress Copyright and version number message
  48. /W4 # Warning level 4
  49. /WX # Warnings as errors
  50. /permissive- # Conformance with standard
  51. /Zc:preprocessor # Forces preprocessor into conformance mode: https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-170
  52. ###################
  53. # Disabled warnings (please do not disable any others without first consulting sig-build)
  54. ###################
  55. /wd4201 # nonstandard extension used: nameless struct/union. This actually became part of the C++11 std, MS has an open issue: https://developercommunity.visualstudio.com/t/warning-level-4-generates-a-bogus-warning-c4201-no/103064
  56. /wd4324 # warning C4324: 'std::tuple<...>': structure was padded due to alignment specifier. This warning is triggered whenever a simd type is used with the MSVC std::optional or std::tuple types, which is namespaced into AZStd
  57. /wd4251 # Don't warn if a class with dllexport attribute has nonstatic members which don't have the dllexport attribute
  58. ###################
  59. # Enabled warnings (that are disabled by default from /W4)
  60. ###################
  61. # https://docs.microsoft.com/en-us/cpp/preprocessor/compiler-warnings-that-are-off-by-default?view=vs-2019
  62. /we4263 # 'function': member function does not override any base class virtual member function
  63. /we4264 # 'virtual_function': no override available for virtual member function from base 'class'; function is hidden
  64. /we4265 # 'class': class has virtual functions, but destructor is not virtual
  65. /we4266 # 'function': no override available for virtual member function from base 'type'; function is hidden
  66. /we4296 # 'operator': expression is always false
  67. /we4426 # optimization flags changed after including header, may be due to #pragma optimize()
  68. /we4437 # dynamic_cast from virtual base 'class1' to 'class2' could fail in some contexts
  69. #/we4619 # #pragma warning: there is no warning number 'number'. Unfortunately some versions of MSVC 16.X dont filter this warning coming from external headers and Qt has a bad warning in QtCore/qvector.h(340,12)
  70. /we4774 # 'string' : format string expected in argument number is not a string literal
  71. /we4777 # 'function' : format string 'string' requires an argument of type 'type1', but variadic argument number has type 'type2
  72. /we5031 # #pragma warning(pop): likely mismatch, popping warning state pushed in different file
  73. /we5032 # detected #pragma warning(push) with no corresponding #pragma warning(pop)
  74. /we5233 # explicit lambda capture 'identifier' is not used
  75. /Zc:forScope # Force Conformance in for Loop Scope
  76. /diagnostics:caret # Compiler diagnostic options: includes the column where the issue was found and places a caret (^) under the location in the line of code where the issue was detected.
  77. /Zc:__cplusplus
  78. /Zc:lambda # Use the new lambda processor (See https://developercommunity.visualstudio.com/t/A-lambda-that-binds-the-this-pointer-w/1467873 for more details)
  79. /favor:AMD64 # Create Code optimized for 64 bit
  80. /bigobj # Increase number of sections in obj files. Profiling has shown no meaningful impact in memory nore build times
  81. COMPILATION_DEBUG
  82. /GS # Enable Buffer security check
  83. /MDd # defines _DEBUG, _MT, and _DLL and causes the application to use the debug multithread-specific and DLL-specific version of the run-time library.
  84. # It also causes the compiler to place the library name MSVCRTD.lib into the .obj file.
  85. /Ob0 # Disables inline expansions
  86. /Od # Disables optimization
  87. COMPILATION_PROFILE
  88. /GF # Enable string pooling
  89. /Gy # Function level linking
  90. /MD # Causes the application to use the multithread-specific and DLL-specific version of the run-time library. Defines _MT and _DLL and causes the compiler
  91. # to place the library name MSVCRT.lib into the .obj file.
  92. /O2 # Maximinize speed, equivalent to /Og /Oi /Ot /Oy /Ob2 /GF /Gy
  93. /Zc:inline # Removes unreferenced functions or data that are COMDATs or only have internal linkage
  94. /Zc:wchar_t # Use compiler native wchar_t
  95. COMPILATION_RELEASE
  96. /Ox # Full optimization
  97. /Ob2 # Inline any suitable function
  98. /Ot # Favor fast code over small code
  99. /Oi # Use Intrinsic Functions
  100. /Oy # Omit the frame pointer
  101. LINK
  102. /NOLOGO # Suppress Copyright and version number message
  103. /IGNORE:4099 # 3rdParty linking produces noise with LNK4099
  104. LINK_NON_STATIC_PROFILE
  105. /OPT:REF # Eliminates functions and data that are never referenced
  106. /OPT:ICF # Perform identical COMDAT folding. Redundant COMDATs can be removed from the linker output
  107. /INCREMENTAL:NO
  108. /DEBUG # Generate pdbs
  109. LINK_NON_STATIC_RELEASE
  110. /OPT:REF # Eliminates functions and data that are never referenced
  111. /OPT:ICF # Perform identical COMDAT folding. Redundant COMDATs can be removed from the linker output
  112. /INCREMENTAL:NO
  113. )
  114. # Look for O3DE_ENABLE_COMPILER_CACHE as a CMake flag or environment variable, then sets the appropriate compatible flags for caching
  115. # More details about the compiler cache can be found in CompilerCache.cmake
  116. if((O3DE_ENABLE_COMPILER_CACHE OR "$ENV{O3DE_ENABLE_COMPILER_CACHE}" STREQUAL "true") AND NOT O3DE_SCRIPT_ONLY)
  117. o3de_compiler_cache_activation() # Activates the compiler cache
  118. # Configure debug info format and compiler launcher for cache compatibility
  119. cmake_policy(SET CMP0141 NEW)
  120. set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "Embedded")
  121. set(CMAKE_C_COMPILER_LAUNCHER ${CMAKE_BINARY_DIR}/cl.exe)
  122. set(CMAKE_CXX_COMPILER_LAUNCHER ${CMAKE_BINARY_DIR}/cl.exe)
  123. # Fallback to compiler flags if the debug format doesn't work, which can depend on CMake version
  124. ly_append_configurations_options(
  125. COMPILATION_PROFILE
  126. /Z7 # Use embedded debug info instead of PDB
  127. COMPILATION_RELEASE
  128. /Z7
  129. )
  130. # Set required VS globals for compiler cache
  131. set(CMAKE_VS_GLOBALS
  132. "CLToolExe=cl.exe"
  133. "CLToolPath=${CMAKE_BINARY_DIR}"
  134. )
  135. else()
  136. ly_append_configurations_options(
  137. COMPILATION_PROFILE
  138. /Zi # Generate debugging information (no Edit/Continue)
  139. )
  140. endif()
  141. set(LY_BUILD_WITH_ADDRESS_SANITIZER FALSE CACHE BOOL "Builds using AddressSanitizer (ASan). Will disable Edit/Continue, Incremental building and Run-Time checks (default = FALSE)")
  142. if(LY_BUILD_WITH_ADDRESS_SANITIZER)
  143. set(LY_BUILD_WITH_INCREMENTAL_LINKING_DEBUG FALSE)
  144. ly_append_configurations_options(
  145. COMPILATION_DEBUG
  146. /fsanitize=address
  147. )
  148. get_filename_component(link_tools_dir ${CMAKE_LINKER} DIRECTORY)
  149. file(COPY
  150. ${link_tools_dir}/clang_rt.asan_dbg_dynamic-x86_64.dll
  151. DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG})
  152. else()
  153. ly_append_configurations_options(
  154. COMPILATION_DEBUG
  155. /RTCsu # Run-Time Error Checks: c Reports when a value is assigned to a smaller data type and results in a data loss (Not supoported by the STL)
  156. # s Enables stack frame run-time error checking
  157. # u Reports when a variable is used without having been initialized
  158. )
  159. endif()
  160. set(LY_BUILD_WITH_INCREMENTAL_LINKING_DEBUG FALSE CACHE BOOL "Indicates if incremental linking is used in debug configurations (default = FALSE)")
  161. if(LY_BUILD_WITH_INCREMENTAL_LINKING_DEBUG)
  162. ly_append_configurations_options(
  163. COMPILATION_DEBUG
  164. /ZI # Enable Edit/Continue
  165. )
  166. else()
  167. # Disable incremental linking
  168. ly_append_configurations_options(
  169. COMPILATION_DEBUG
  170. /Zi # Generate debugging information (no Edit/Continue). Edit/Continue requires incremental linking
  171. LINK_NON_STATIC_DEBUG
  172. /DEBUG # Despite the documentation states /Zi implies /DEBUG, without it, stack traces are not expanded
  173. /INCREMENTAL:NO
  174. )
  175. endif()
  176. set(O3DE_BUILD_WITH_DEBUG_SYMBOLS_RELEASE FALSE CACHE BOOL "Add debug symbols when building in release configuration. (default = FALSE)")
  177. if(O3DE_BUILD_WITH_DEBUG_SYMBOLS_RELEASE)
  178. ly_append_configurations_options(
  179. COMPILATION_RELEASE
  180. /Od # Enable debug symbols
  181. /Zi # Generate debugging information (no Edit/Continue)
  182. LINK_NON_STATIC_RELEASE
  183. /DEBUG # Generate pdbs
  184. )
  185. endif()
  186. # Configure system includes
  187. ly_set(LY_CXX_SYSTEM_INCLUDE_CONFIGURATION_FLAG
  188. /experimental:external # Turns on "external" headers feature for MSVC compilers, required for MSVC < 16.10
  189. /external:W0 # Set warning level in external headers to 0. This is used to suppress warnings 3rdParty libraries which uses the "system_includes" option in their json configuration
  190. )
  191. # CMake 3.22rc added a definition for CMAKE_INCLUDE_SYSTEM_FLAG_CXX. However, its defined as "-external:I ", that space causes
  192. # issues when trying to use in TargetIncludeSystemDirectories_unsupported.cmake.
  193. # CMake 3.22rc has also not added support for external directories in MSVC through target_include_directories(... SYSTEM
  194. # So we will just fix the flag that was added by 3.22rc so it works with our TargetIncludeSystemDirectories_unsupported.cmake
  195. # Once target_include_directories(... SYSTEM is supported, we can branch and use TargetIncludeSystemDirectories_supported.cmake
  196. # Reported this here: https://gitlab.kitware.com/cmake/cmake/-/issues/17904#note_1078281
  197. if(NOT CMAKE_INCLUDE_SYSTEM_FLAG_CXX)
  198. ly_set(CMAKE_INCLUDE_SYSTEM_FLAG_CXX "/external:I")
  199. else()
  200. string(STRIP ${CMAKE_INCLUDE_SYSTEM_FLAG_CXX} CMAKE_INCLUDE_SYSTEM_FLAG_CXX)
  201. endif()
  202. include(cmake/Platform/Common/TargetIncludeSystemDirectories_unsupported.cmake)
  203. if(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION VERSION_LESS_EQUAL "10.0.19041.0")
  204. # Suppresses warning C5105 which triggers with Windows 10 SDK 10.0.19041 and below when using the /Zc:preprocessor option
  205. # https://developercommunity.visualstudio.com/t/stdc17-generates-warning-compiling-windowsh/1249671
  206. ly_append_configurations_options(
  207. COMPILATION
  208. /wd5104
  209. /wd5105
  210. )
  211. endif()