From 7a42bc7ad3d1c30f79af5214e413046c52055191 Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Tue, 2 Dec 2025 16:24:27 -0800 Subject: [PATCH] cmake: rename MOMO_BUILD_AS_LIBRARY to SOGEN_BUILD_STATIC Also adds FATAL_ERROR guard when SOGEN_STATIC_CRT=ON without SOGEN_BUILD_STATIC=ON, since static CRT with shared libraries causes heap corruption (each DLL gets its own allocator but sogen passes ownership across boundaries). These options are designed to be used together for full static linking, useful for embedding sogen in projects like IDA Pro plugins. --- CMakeLists.txt | 2 +- cmake/compiler-env.cmake | 13 ++++++++++--- src/CMakeLists.txt | 2 +- src/backends/icicle-emulator/CMakeLists.txt | 2 +- .../icicle-emulator/icicle_x86_64_emulator.hpp | 2 +- src/backends/unicorn-emulator/CMakeLists.txt | 2 +- .../unicorn-emulator/unicorn_x86_64_emulator.hpp | 2 +- 7 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 342aa659..8415adfb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,7 @@ endif() ########################################## -option(MOMO_BUILD_AS_LIBRARY "Configure and Build the sogen as a shared library (without the samples and tests)" ${MOMO_IS_SUBPROJECT}) +option(SOGEN_BUILD_STATIC "Build sogen as static libraries for embedding (e.g., IDA plugins)" ${MOMO_IS_SUBPROJECT}) ########################################## diff --git a/cmake/compiler-env.cmake b/cmake/compiler-env.cmake index a4b85e54..4ea6fe60 100644 --- a/cmake/compiler-env.cmake +++ b/cmake/compiler-env.cmake @@ -31,10 +31,10 @@ endif() ########################################## -if(MOMO_BUILD_AS_LIBRARY) - add_compile_definitions(MOMO_BUILD_AS_LIBRARY=1) +if(SOGEN_BUILD_STATIC) + add_compile_definitions(SOGEN_BUILD_STATIC=1) else() - add_compile_definitions(MOMO_BUILD_AS_LIBRARY=0) + add_compile_definitions(SOGEN_BUILD_STATIC=0) endif() ########################################## @@ -230,6 +230,13 @@ endif() option(SOGEN_STATIC_CRT "Use static CRT (/MT) instead of dynamic (/MD)" OFF) +if(SOGEN_STATIC_CRT AND NOT SOGEN_BUILD_STATIC) + message(FATAL_ERROR + "SOGEN_STATIC_CRT=ON requires SOGEN_BUILD_STATIC=ON.\n" + "Static CRT with shared libraries causes heap corruption - " + "each DLL gets its own allocator, but sogen passes ownership across boundaries.") +endif() + if(SOGEN_STATIC_CRT) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") elseif(DEFINED CMAKE_MSVC_RUNTIME_LIBRARY) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 300b763d..3ddaf3a2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,7 +8,7 @@ add_subdirectory(backend-selection) momo_add_subdirectory_and_get_targets("backends" BACKEND_TARGETS) momo_targets_set_folder("backends" ${BACKEND_TARGETS}) -if (NOT MOMO_BUILD_AS_LIBRARY) +if (NOT SOGEN_BUILD_STATIC) add_subdirectory(analyzer) add_subdirectory(debugger) add_subdirectory(fuzzing-engine) diff --git a/src/backends/icicle-emulator/CMakeLists.txt b/src/backends/icicle-emulator/CMakeLists.txt index da62e3af..72cd7396 100644 --- a/src/backends/icicle-emulator/CMakeLists.txt +++ b/src/backends/icicle-emulator/CMakeLists.txt @@ -8,7 +8,7 @@ file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS list(SORT SRC_FILES) -if(MOMO_BUILD_AS_LIBRARY) +if(SOGEN_BUILD_STATIC) add_library(icicle-emulator STATIC ${SRC_FILES}) else() add_library(icicle-emulator SHARED ${SRC_FILES}) diff --git a/src/backends/icicle-emulator/icicle_x86_64_emulator.hpp b/src/backends/icicle-emulator/icicle_x86_64_emulator.hpp index bc2ab6fb..4bf121b4 100644 --- a/src/backends/icicle-emulator/icicle_x86_64_emulator.hpp +++ b/src/backends/icicle-emulator/icicle_x86_64_emulator.hpp @@ -12,7 +12,7 @@ namespace icicle { -#if !MOMO_BUILD_AS_LIBRARY +#if !SOGEN_BUILD_STATIC ICICLE_EMULATOR_DLL_STORAGE #endif std::unique_ptr create_x86_64_emulator(); diff --git a/src/backends/unicorn-emulator/CMakeLists.txt b/src/backends/unicorn-emulator/CMakeLists.txt index 8b44e286..c2b24557 100644 --- a/src/backends/unicorn-emulator/CMakeLists.txt +++ b/src/backends/unicorn-emulator/CMakeLists.txt @@ -6,7 +6,7 @@ file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS list(SORT SRC_FILES) -if(MOMO_BUILD_AS_LIBRARY) +if(SOGEN_BUILD_STATIC) add_library(unicorn-emulator STATIC ${SRC_FILES}) else() add_library(unicorn-emulator SHARED ${SRC_FILES}) diff --git a/src/backends/unicorn-emulator/unicorn_x86_64_emulator.hpp b/src/backends/unicorn-emulator/unicorn_x86_64_emulator.hpp index a64b399f..84fbf62f 100644 --- a/src/backends/unicorn-emulator/unicorn_x86_64_emulator.hpp +++ b/src/backends/unicorn-emulator/unicorn_x86_64_emulator.hpp @@ -12,7 +12,7 @@ namespace unicorn { -#if !MOMO_BUILD_AS_LIBRARY +#if !SOGEN_BUILD_STATIC UNICORN_EMULATOR_DLL_STORAGE #endif std::unique_ptr create_x86_64_emulator();