windows.cmake 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #[=======================================================================[.rst:
  2. Windows
  3. -------
  4. This file contains functions for options and configuration for targeting the
  5. Windows platform
  6. Because this file is included into the top level CMakelists.txt before the
  7. project directive, it means that
  8. * ``CMAKE_CURRENT_SOURCE_DIR`` is the location of godot-cpp's CMakeLists.txt
  9. * ``CMAKE_SOURCE_DIR`` is the location where any prior ``project(...)``
  10. directive was
  11. MSVC Runtime Selection
  12. ----------------------
  13. There are two main ways to set the msvc runtime library;
  14. Using ``target_compile_options()`` to add the flags
  15. or using the ``CMAKE_MSVC_RUNTIME_LIBRARY`` property_ abstraction, introduced
  16. in CMake version 3.15 with the policy CMP0091_ to remove the flags from
  17. ``CMAKE_<LANG>_FLAGS_<CONFIG>``.
  18. Default: ``CMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"``
  19. This initializes each target's ``MSVC_RUNTIME_LIBRARY`` property at the time of
  20. target creation.
  21. it is stated in the msvc_ documentation that: "All modules passed to a given
  22. invocation of the linker must have been compiled with the same runtime library
  23. compiler option (/MD, /MT, /LD)."
  24. This creates a conundrum for us, the ``CMAKE_MSVC_RUNTIME_LIBRARY`` needs to be
  25. correct at the time the target is created, but we have no control over the
  26. consumers CMake scripts, and the per-target ``MSVC_RUNTIME_LIBRARY`` property
  27. is not transient.
  28. It has been raised that not using ``CMAKE_MSVC_RUNTIME_LIBRARY`` can also cause
  29. issues_ when a dependency( independent to godot-cpp ) that doesn't set any
  30. runtime flags, which relies purely on the ``CMAKE_MSVC_RUNTIME_LIBRARY``
  31. variable will very likely not have the correct msvc runtime flags set.
  32. So we'll set ``CMAKE_MSVC_RUNTIME_LIBRARY`` as CACHE STRING so that it will be
  33. available for consumer target definitions, but also be able to be overridden if
  34. needed.
  35. Additionally we message consumers notifying them and pointing to this
  36. documentation.
  37. .. _CMP0091:https://cmake.org/cmake/help/latest/policy/CMP0091.html
  38. .. _property:https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
  39. .. https://discourse.cmake.org/t/mt-staticrelease-doesnt-match-value-md-dynamicrelease/5428/4
  40. .. _msvc: https://learn.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library
  41. .. _issues: https://github.com/godotengine/godot-cpp/issues/1699
  42. ]=======================================================================]
  43. function( windows_options )
  44. option( GODOTCPP_USE_STATIC_CPP "Link MinGW/MSVC C++ runtime libraries statically" ON )
  45. option( GODOTCPP_DEBUG_CRT "Compile with MSVC's debug CRT (/MDd)" OFF )
  46. message( STATUS "If not already cached, setting CMAKE_MSVC_RUNTIME_LIBRARY.\n"
  47. "\tFor more information please read godot-cpp/cmake/windows.cmake")
  48. set( CMAKE_MSVC_RUNTIME_LIBRARY
  49. "MultiThreaded$<IF:$<BOOL:${GODOTCPP_DEBUG_CRT}>,DebugDLL,$<$<NOT:$<BOOL:${GODOTCPP_USE_STATIC_CPP}>>:DLL>>"
  50. CACHE STRING "Select the MSVC runtime library for use by compilers targeting the MSVC ABI.")
  51. endfunction()
  52. #[===========================[ Target Generation ]===========================]
  53. function( windows_generate )
  54. set( STATIC_CPP "$<BOOL:${GODOTCPP_USE_STATIC_CPP}>")
  55. set_target_properties( ${TARGET_NAME}
  56. PROPERTIES
  57. PDB_OUTPUT_DIRECTORY "$<1:${CMAKE_SOURCE_DIR}/bin>"
  58. )
  59. target_compile_definitions( ${TARGET_NAME}
  60. PUBLIC
  61. WINDOWS_ENABLED
  62. $<${IS_MSVC}:
  63. TYPED_METHOD_BIND
  64. NOMINMAX
  65. >
  66. )
  67. target_link_options( ${TARGET_NAME}
  68. PUBLIC
  69. $<${NOT_MSVC}:
  70. -Wl,--no-undefined
  71. $<${STATIC_CPP}:
  72. -static
  73. -static-libgcc
  74. -static-libstdc++
  75. >
  76. >
  77. $<${IS_CLANG}:-lstdc++>
  78. )
  79. common_compiler_flags()
  80. endfunction()