From c58abdc5fe884a0869ebe81924ce2bfb7faa6c91 Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Sun, 12 Jan 2025 11:43:02 -0800 Subject: [PATCH 1/3] added MOMO_BUILD_AS_LIBRARY this allows to build the emulator as a static library. useful for using the project as a library --- CMakeLists.txt | 1 + src/CMakeLists.txt | 16 +++++++++------- src/unicorn-emulator/CMakeLists.txt | 6 +++++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f15684c7..c858a949 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.26.4) ########################################## option(MOMO_ENABLE_SANITIZER "Enable sanitizer" OFF) +option(MOMO_BUILD_AS_LIBRARY "Configure and Build the emulator as a shared library (without the samples and tests)" OFF) ########################################## diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea61b41b..ff79e18f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,11 +2,13 @@ add_subdirectory(common) add_subdirectory(emulator) add_subdirectory(unicorn-emulator) add_subdirectory(windows-emulator) -add_subdirectory(analyzer) -add_subdirectory(fuzzing-engine) -add_subdirectory(fuzzer) -if(WIN32) - add_subdirectory(bad-sample) - add_subdirectory(test-sample) +if (NOT MOMO_BUILD_AS_LIBRARY) + add_subdirectory(analyzer) + add_subdirectory(fuzzing-engine) + add_subdirectory(fuzzer) + if(WIN32) + add_subdirectory(bad-sample) + add_subdirectory(test-sample) + endif() + add_subdirectory(windows-emulator-test) endif() -add_subdirectory(windows-emulator-test) diff --git a/src/unicorn-emulator/CMakeLists.txt b/src/unicorn-emulator/CMakeLists.txt index 3b473f3b..80df01fe 100644 --- a/src/unicorn-emulator/CMakeLists.txt +++ b/src/unicorn-emulator/CMakeLists.txt @@ -6,7 +6,11 @@ file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS list(SORT SRC_FILES) -add_library(unicorn-emulator SHARED ${SRC_FILES}) +if(MOMO_BUILD_AS_LIBRARY) + add_library(unicorn-emulator STATIC ${SRC_FILES}) +else() + add_library(unicorn-emulator SHARED ${SRC_FILES}) +endif() target_include_directories(unicorn-emulator INTERFACE "${CMAKE_CURRENT_LIST_DIR}" From 051e7a77a7e054d0312fe2bb0da8170930926880 Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Sun, 12 Jan 2025 11:56:11 -0800 Subject: [PATCH 2/3] no need to export anything if MOMO_BUILD_AS_LIBRARY --- CMakeLists.txt | 6 ++++++ src/unicorn-emulator/unicorn_x64_emulator.hpp | 2 ++ 2 files changed, 8 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index c858a949..02e43e14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,12 @@ cmake_minimum_required(VERSION 3.26.4) option(MOMO_ENABLE_SANITIZER "Enable sanitizer" OFF) option(MOMO_BUILD_AS_LIBRARY "Configure and Build the emulator as a shared library (without the samples and tests)" OFF) +if(MOMO_BUILD_AS_LIBRARY) + add_compile_definitions(MOMO_BUILD_AS_LIBRARY=1) +else() + add_compile_definitions(MOMO_BUILD_AS_LIBRARY=0) +endif() + ########################################## set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) diff --git a/src/unicorn-emulator/unicorn_x64_emulator.hpp b/src/unicorn-emulator/unicorn_x64_emulator.hpp index f0535a56..6d5bd48e 100644 --- a/src/unicorn-emulator/unicorn_x64_emulator.hpp +++ b/src/unicorn-emulator/unicorn_x64_emulator.hpp @@ -12,6 +12,8 @@ namespace unicorn { +#if !MOMO_BUILD_AS_LIBRARY UNICORN_EMULATOR_DLL_STORAGE +#endif std::unique_ptr create_x64_emulator(); } From a16c1dd65417c5821213acbe7eb0b0403ba60aeb Mon Sep 17 00:00:00 2001 From: Elias Bachaalany Date: Sun, 12 Jan 2025 12:31:53 -0800 Subject: [PATCH 3/3] renamed dependency 'common' to emulator-common also made it mandatory when using the 'windows-emulator' dep --- src/analyzer/CMakeLists.txt | 1 - src/bad-sample/CMakeLists.txt | 2 +- src/common/CMakeLists.txt | 6 +++--- src/fuzzer/CMakeLists.txt | 1 - src/fuzzing-engine/CMakeLists.txt | 2 +- src/unicorn-emulator/CMakeLists.txt | 2 +- src/windows-emulator-test/CMakeLists.txt | 1 - src/windows-emulator/CMakeLists.txt | 2 +- 8 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/analyzer/CMakeLists.txt b/src/analyzer/CMakeLists.txt index 13b46b08..54c497d1 100644 --- a/src/analyzer/CMakeLists.txt +++ b/src/analyzer/CMakeLists.txt @@ -13,7 +13,6 @@ momo_assign_source_group(${SRC_FILES}) target_precompile_headers(analyzer PRIVATE std_include.hpp) target_link_libraries(analyzer PRIVATE - common reflect windows-emulator ) diff --git a/src/bad-sample/CMakeLists.txt b/src/bad-sample/CMakeLists.txt index 2e6e2502..eb1d6316 100644 --- a/src/bad-sample/CMakeLists.txt +++ b/src/bad-sample/CMakeLists.txt @@ -7,6 +7,6 @@ file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS list(SORT SRC_FILES) add_executable(bad-sample ${SRC_FILES}) -target_link_libraries(bad-sample PRIVATE common) +target_link_libraries(bad-sample PRIVATE emulator-common) momo_assign_source_group(${SRC_FILES}) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index dd09f43b..42753b78 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -3,14 +3,14 @@ file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS *.hpp ) -add_library(common ${SRC_FILES}) +add_library(emulator-common ${SRC_FILES}) momo_assign_source_group(${SRC_FILES}) -target_include_directories(common INTERFACE "${CMAKE_CURRENT_LIST_DIR}") +target_include_directories(emulator-common INTERFACE "${CMAKE_CURRENT_LIST_DIR}") set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) -target_link_libraries(common PUBLIC +target_link_libraries(emulator-common PUBLIC Threads::Threads ) diff --git a/src/fuzzer/CMakeLists.txt b/src/fuzzer/CMakeLists.txt index a06cae8b..8a7b600a 100644 --- a/src/fuzzer/CMakeLists.txt +++ b/src/fuzzer/CMakeLists.txt @@ -13,7 +13,6 @@ momo_assign_source_group(${SRC_FILES}) target_precompile_headers(fuzzer PRIVATE std_include.hpp) target_link_libraries(fuzzer PRIVATE - common fuzzing-engine windows-emulator ) diff --git a/src/fuzzing-engine/CMakeLists.txt b/src/fuzzing-engine/CMakeLists.txt index bda32d22..90ecd42b 100644 --- a/src/fuzzing-engine/CMakeLists.txt +++ b/src/fuzzing-engine/CMakeLists.txt @@ -11,7 +11,7 @@ add_library(fuzzing-engine ${SRC_FILES}) momo_assign_source_group(${SRC_FILES}) target_link_libraries(fuzzing-engine PRIVATE - common + emulator-common ) target_include_directories(fuzzing-engine INTERFACE diff --git a/src/unicorn-emulator/CMakeLists.txt b/src/unicorn-emulator/CMakeLists.txt index 80df01fe..f86f2870 100644 --- a/src/unicorn-emulator/CMakeLists.txt +++ b/src/unicorn-emulator/CMakeLists.txt @@ -17,4 +17,4 @@ target_include_directories(unicorn-emulator INTERFACE ) target_link_libraries(unicorn-emulator PUBLIC emulator) -target_link_libraries(unicorn-emulator PRIVATE unicorn common) +target_link_libraries(unicorn-emulator PRIVATE unicorn emulator-common) diff --git a/src/windows-emulator-test/CMakeLists.txt b/src/windows-emulator-test/CMakeLists.txt index 42f1934b..0bffacc1 100644 --- a/src/windows-emulator-test/CMakeLists.txt +++ b/src/windows-emulator-test/CMakeLists.txt @@ -12,7 +12,6 @@ momo_assign_source_group(${SRC_FILES}) target_link_libraries(windows-emulator-test PRIVATE gtest - common windows-emulator ) diff --git a/src/windows-emulator/CMakeLists.txt b/src/windows-emulator/CMakeLists.txt index 0fb26ae9..b2ffe79c 100644 --- a/src/windows-emulator/CMakeLists.txt +++ b/src/windows-emulator/CMakeLists.txt @@ -13,12 +13,12 @@ momo_assign_source_group(${SRC_FILES}) target_precompile_headers(windows-emulator PRIVATE std_include.hpp) target_link_libraries(windows-emulator PRIVATE - common unicorn-emulator mini-gdbstub ) target_link_libraries(windows-emulator PUBLIC + emulator-common emulator )