Skip to content

cmake: add SNAPPY_MSVC_STATIC_RUNTIME and enable CMP0091 (fixes #190) - #260

Open
jdymitarai wants to merge 1 commit into
google:mainfrom
jdymitarai:feat-msvc-static-runtime-190
Open

cmake: add SNAPPY_MSVC_STATIC_RUNTIME and enable CMP0091 (fixes #190)#260
jdymitarai wants to merge 1 commit into
google:mainfrom
jdymitarai:feat-msvc-static-runtime-190

Conversation

@jdymitarai

Copy link
Copy Markdown

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 (/MT or /MTd) rather than the dynamic CRT DLL (/MD or /MDd).

Because Snappy's cmake_minimum_required is 3.10, CMake policy CMP0091 defaults to OLD. Under the old policy, /MD is hardcoded into CMAKE_<LANG>_FLAGS, making it difficult to select the static CRT and leading to compiler flag conflicts or linker warnings.

Solution

  1. Set CMake policy CMP0091 to NEW if available (CMake >= 3.15), enabling standard MSVC runtime library selection via CMAKE_MSVC_RUNTIME_LIBRARY.
  2. Introduce the SNAPPY_MSVC_STATIC_RUNTIME CMake option (default OFF to preserve existing behavior).
  3. When enabled, sets CMAKE_MSVC_RUNTIME_LIBRARY to "MultiThreaded$<$<CONFIG:Debug>:Debug>" and replaces any default /MD flags with /MT across all build configurations for backward compatibility with older CMake versions.

Verification

  • Configured and built with -DSNAPPY_MSVC_STATIC_RUNTIME=ON -DSNAPPY_BUILD_TESTS=ON under MSVC 2022.
  • Verified via dumpbin /dependents snappy_unittest.exe that dependencies on MSVCP140.dll and VCRUNTIME140.dll are eliminated.
  • Ran snappy_unittest.exe: all 25 unit tests pass.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add a cmake option MSVC_RUNTIME_LIBRARY

1 participant