windows.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #[============================[ Windows Options ]============================]
  44. function(windows_options)
  45. #[[ Options from SCons
  46. TODO silence_msvc: Silence MSVC's cl/link stdout bloat, redirecting errors to stderr
  47. Default: True
  48. These three options will not implemented as compiler selection is managed
  49. by CMake toolchain files. Look to doc/cmake.rst for examples.
  50. use_mingw: Use the MinGW compiler instead of MSVC - only effective on Windows
  51. use_llvm: Use the LLVM compiler (MVSC or MinGW depending on the use_mingw flag
  52. mingw_prefix: MinGW prefix
  53. ]]
  54. option(GODOTCPP_USE_STATIC_CPP "Link MinGW/MSVC C++ runtime libraries statically" ON)
  55. option(GODOTCPP_DEBUG_CRT "Compile with MSVC's debug CRT (/MDd)" OFF)
  56. message(
  57. STATUS
  58. "If not already cached, setting CMAKE_MSVC_RUNTIME_LIBRARY.\n"
  59. "\tFor more information please read godot-cpp/cmake/windows.cmake"
  60. )
  61. set(CMAKE_MSVC_RUNTIME_LIBRARY
  62. "MultiThreaded$<IF:$<BOOL:${GODOTCPP_DEBUG_CRT}>,DebugDLL,$<$<NOT:$<BOOL:${GODOTCPP_USE_STATIC_CPP}>>:DLL>>"
  63. CACHE STRING
  64. "Select the MSVC runtime library for use by compilers targeting the MSVC ABI."
  65. )
  66. endfunction()
  67. #[===========================[ Target Generation ]===========================]
  68. function(windows_generate)
  69. set(STATIC_CPP "$<BOOL:${GODOTCPP_USE_STATIC_CPP}>")
  70. set_target_properties(godot-cpp PROPERTIES PDB_OUTPUT_DIRECTORY "$<1:${CMAKE_SOURCE_DIR}/bin>")
  71. target_compile_definitions(
  72. godot-cpp
  73. PUBLIC WINDOWS_ENABLED $<${IS_MSVC}: TYPED_METHOD_BIND NOMINMAX >
  74. )
  75. # gersemi: off
  76. target_link_options(
  77. godot-cpp
  78. PUBLIC
  79. $<${NOT_MSVC}:
  80. -Wl,--no-undefined
  81. $<${STATIC_CPP}:
  82. -static
  83. -static-libgcc
  84. -static-libstdc++
  85. >
  86. >
  87. $<${IS_CLANG}:-lstdc++>
  88. )
  89. # gersemi: on
  90. common_compiler_flags()
  91. endfunction()