godotengine / godotengine/godot-cpp

Broken logic in MSVC runtime selection?

Open
#1,954 3 comments 0 reactions 1 assignee Claimed by @enetheru View on GitHub
bug cmake
Dominant language
C++
Stars
2.7k
Forks
809
Avg merge
1d 3h
Merged PRs (30d)
8

Description

### 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$<$:Debug>$<$>: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

```cmake
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")
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.