Fix: [MSVC] generate static libraries / executables correctly with vcpkg -static target #11208
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Problem
When building for MSVC I was often warned that
/MDd
(dynamic) was overwriting/MT
(static), which surprised me, as we should be/MT
. Now this was never the problem with our current libraries, and things were linked in statically anyway, but when addingbreakpad
for testing, it became apparent that we were actually compiling/MDd
. But with the vcpkg targetx64-windows-static
I expected/MTd
.Description
Since CMake 3.15 (our minimum is 3.16) there is a CMake variable https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html to indicate whether
/MD
or/MT
should be used. Seems in more recent MSVC and CMake this is finally handled properly, and MSVC was happily compiling the binary dynamic.By setting it correctly, the
/MTd
is correctly added instead of the/MDd
(well, it is-MTd
and-MDd
, but who is counting). The best way to validate this is to make an error in a file, and check thecl.exe
line.In case you wonder if it is actually the intention to build static, please see https://github.com/OpenTTD/OpenTTD/blob/master/cmake/CompileFlags.cmake#L8 . We already tried .. but that no longer works for the latest CMake / MSVC combo.
Limitations
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.