From d4166a5c8d843d9a1ee3bf4522e165e22f399db9 Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Wed, 26 Nov 2025 17:56:48 -0800 Subject: [PATCH] cmake: add SOGEN_STATIC_CRT option for static runtime linking Add option to use static CRT (/MT) instead of dynamic (/MD) for projects that require static linking, such as IDA Pro plugins. - Default remains /MD (no change for existing users) - Set -DSOGEN_STATIC_CRT=ON to use /MT - Also respects parent's CMAKE_MSVC_RUNTIME_LIBRARY if already set Includes warning about potential heap corruption when allocations cross library boundaries with static CRT. --- cmake/compiler-env.cmake | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/cmake/compiler-env.cmake b/cmake/compiler-env.cmake index 6ec71ddc..a4b85e54 100644 --- a/cmake/compiler-env.cmake +++ b/cmake/compiler-env.cmake @@ -217,10 +217,26 @@ if(MOMO_ENABLE_SANITIZER) endif() ########################################## -# Must be a dynamic runtime (/MD or /MDd) to enforce -# shared allocators between emulator and implementation +# MSVC Runtime Library Selection +# +# Default is dynamic runtime (/MD or /MDd) to enforce shared allocators +# between emulator and implementation. +# +# Use SOGEN_STATIC_CRT=ON for static runtime (/MT or /MTd) when embedding +# in projects that require it (e.g., IDA plugins). +# +# WARNING: Static CRT may cause heap corruption if memory is allocated +# in one module and freed in another. Ensure allocation ownership is clear. -set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$:Debug>DLL) +option(SOGEN_STATIC_CRT "Use static CRT (/MT) instead of dynamic (/MD)" OFF) + +if(SOGEN_STATIC_CRT) + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") +elseif(DEFINED CMAKE_MSVC_RUNTIME_LIBRARY) + # Respect parent project's setting +else() + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") +endif() ##########################################