Skip to content

Broken logic in MSVC runtime selection? #1954

@jcelerier

Description

@jcelerier

Godot version

4.5

godot-cpp version

4.5

System information

Windows w/MSVC

Issue description

The logic about the CRT in windows.cmake seems flawed: it requires users to add new build flags to counteract the setting of CMAKE_MSVC_RUNTIME_LIBRARY when including godot-cpp somewhere in their project versus just letting the CMake defaults.

As a CMake user, I really don't want libraries I include to fiddle with global CMake options, this is the responsibility of the end-user to do this explicitly ; doing otherwise really breaks expectations.

To keep the GODOTCPP_USE_STATIC_CPP, what I would expect instead here would be:

set(CMAKE_MSVC_RUNTIME_LIBRARY
    "MultiThreaded$<$<CONFIG:Debug>:Debug>$<$<NOT:$<BOOL:${GODOTCPP_USE_STATIC_CPP}>>:DLL>"
    CACHE STRING
    "Select the MSVC runtime library for use by compilers targeting the MSVC ABI."
)

which will correctly keep Debug builds with the Debug CRT, and support static-debug build (/MTd), e.g. the normal expectation when building in the MSVC ecosystem, unlike what the code currently does which is forcing the Release CRT unless the user sets GODOTCPP_DEBUG_CRT, thus breaking build in a given build system that is not godot-related (as noticed and mentioned in the last paragraph in the cmake file!).
Since godot has a C ABI between DLLs and the engine, this as far as I know should not cause a problem (and also enable more useful debug checks for gdextension autors).

Or at worst, provide a godot_setup_gdextension(TARGET) function that end-users have to call, and which will set MSVC_RUNTIME_LIBRARY on targets that link to godot-cpp without leaking into the rest of a given build system

Steps to reproduce

add_subdirectory(godot-cpp)
add_library(foo SHARED foo.cpp) #oops, cmake --build . --config Debug on MSVC produces foo with the release crt

Minimal reproduction project

cmake_minimum_required(VERSION 4.0 FATAL_ERROR)
project(repro)

include(FetchContent)
FetchContent_Declare(
      godot-cpp
      GIT_REPOSITORY  https://github.com/godotengine/godot-cpp.git
      GIT_TAG         4.5
      GIT_SHALLOW     TRUE
)
FetchContent_MakeAvailable(godot-cpp)

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/foo.cpp" "int main(){}")
add_library(foo SHARED "${CMAKE_CURRENT_BINARY_DIR}/foo.cpp") 

Metadata

Metadata

Assignees

Labels

bugThis has been identified as a bugcmake

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions