cmake: add SNAPPY_MSVC_STATIC_RUNTIME and enable CMP0091 (fixes #190) - #260
Open
jdymitarai wants to merge 1 commit into
Open
cmake: add SNAPPY_MSVC_STATIC_RUNTIME and enable CMP0091 (fixes #190)#260jdymitarai wants to merge 1 commit into
jdymitarai wants to merge 1 commit into
Conversation
…e#190) CMake 3.15 introduced CMP0091 to select MSVC runtime libraries via CMAKE_MSVC_RUNTIME_LIBRARY. Because cmake_minimum_required is 3.10, CMP0091 defaults to OLD, which hardcodes /MD into compiler flags and prevents setting static runtime (/MT) when building static libraries. Enable CMP0091 when available and add the SNAPPY_MSVC_STATIC_RUNTIME option (default OFF), which sets CMAKE_MSVC_RUNTIME_LIBRARY to MultiThreaded and replaces /MD with /MT in flags for backwards compatibility.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #190
Problem
When consuming Snappy as a static library on Windows or building static binaries, users often need to link against the static MSVC runtime (
/MTor/MTd) rather than the dynamic CRT DLL (/MDor/MDd).Because Snappy's
cmake_minimum_requiredis 3.10, CMake policyCMP0091defaults toOLD. Under the old policy,/MDis hardcoded intoCMAKE_<LANG>_FLAGS, making it difficult to select the static CRT and leading to compiler flag conflicts or linker warnings.Solution
CMP0091toNEWif available (CMake >= 3.15), enabling standard MSVC runtime library selection viaCMAKE_MSVC_RUNTIME_LIBRARY.SNAPPY_MSVC_STATIC_RUNTIMECMake option (defaultOFFto preserve existing behavior).CMAKE_MSVC_RUNTIME_LIBRARYto"MultiThreaded$<$<CONFIG:Debug>:Debug>"and replaces any default/MDflags with/MTacross all build configurations for backward compatibility with older CMake versions.Verification
-DSNAPPY_MSVC_STATIC_RUNTIME=ON -DSNAPPY_BUILD_TESTS=ONunder MSVC 2022.dumpbin /dependents snappy_unittest.exethat dependencies onMSVCP140.dllandVCRUNTIME140.dllare eliminated.snappy_unittest.exe: all 25 unit tests pass.