Skip to content

Commit

Permalink
Merge pull request #157 from nathan818fr/fix-capstone-crt
Browse files Browse the repository at this point in the history
Fix MSVC runtime library selection
  • Loading branch information
rdbo authored Dec 9, 2023
2 parents 253496b + 0d55087 commit 7541d5e
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ set(CAPSTONE_CMAKE_ARGS
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DCAPSTONE_ARCHITECTURE_DEFAULT=OFF
)
if(DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
if(${CMAKE_MSVC_RUNTIME_LIBRARY} STREQUAL "MultiThreaded" OR ${CMAKE_MSVC_RUNTIME_LIBRARY} STREQUAL "MultiThreadedDebug")
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DCAPSTONE_BUILD_STATIC_RUNTIME=ON)
if(MSVC)
if(CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreaded" OR CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreadedDebug")
set(CAPSTONE_CMAKE_ARGS ${CAPSTONE_CMAKE_ARGS} -DCAPSTONE_BUILD_STATIC_RUNTIME=ON)
else()
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DCAPSTONE_BUILD_STATIC_RUNTIME=OFF)
set(CAPSTONE_CMAKE_ARGS ${CAPSTONE_CMAKE_ARGS} -DCAPSTONE_BUILD_STATIC_RUNTIME=OFF)
endif()
# Hack: remove hardcoded /MT and /MTd flags from the Capstone v4 CMakeLists.txt
# This way, it will respect CMAKE_MSVC_RUNTIME_LIBRARY and CAPSTONE_BUILD_STATIC_RUNTIME
set(CAPSTONE_CMAKELISTS "${CAPSTONE_DIR}/CMakeLists.txt")
file(LOCK "${CAPSTONE_CMAKELISTS}.lock" TIMEOUT 10)
file(READ "${CAPSTONE_CMAKELISTS}" CAPSTONE_CMAKELISTS_CONTENTS)
string(REPLACE "\"\${CMAKE_CXX_FLAGS_RELEASE} /MT\"" "\"\${CMAKE_CXX_FLAGS_RELEASE}\"" CAPSTONE_CMAKELISTS_CONTENTS "${CAPSTONE_CMAKELISTS_CONTENTS}")
string(REPLACE "\"\${CMAKE_CXX_FLAGS_DEBUG} /MTd\"" "\"\${CMAKE_CXX_FLAGS_DEBUG}\"" CAPSTONE_CMAKELISTS_CONTENTS "${CAPSTONE_CMAKELISTS_CONTENTS}")
file(WRITE "${CAPSTONE_CMAKELISTS}" "${CAPSTONE_CMAKELISTS_CONTENTS}")
file(LOCK "${CAPSTONE_CMAKELISTS}.lock" RELEASE)
else()
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DCAPSTONE_BUILD_STATIC_RUNTIME=OFF)
set(CAPSTONE_CMAKE_ARGS ${CAPSTONE_CMAKE_ARGS} -DCAPSTONE_BUILD_STATIC_RUNTIME=OFF)
endif()
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
set(CAPSTONE_CMAKE_ARGS ${CAPSTONE_CMAKE_ARGS} -DCAPSTONE_ARM64_SUPPORT=ON)
Expand All @@ -38,19 +47,26 @@ set(KEYSTONE_CMAKE_ARGS
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
-DCMAKE_MSVC_RUNTIME_LIBRARY=${CMAKE_MSVC_RUNTIME_LIBRARY}
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DKEYSTONE_BUILD_STATIC_RUNTIME=OFF
)
if(DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
if(MSVC)
string(TOUPPER "LLVM_USE_CRT_${CMAKE_BUILD_TYPE}" LLVM_USE_CRT_VAR)
if(${CMAKE_MSVC_RUNTIME_LIBRARY} STREQUAL "MultiThreaded")
if(CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreaded")
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DKEYSTONE_BUILD_STATIC_RUNTIME=ON)
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -D${LLVM_USE_CRT_VAR}=MT)
elseif(${CMAKE_MSVC_RUNTIME_LIBRARY} STREQUAL "MultiThreadedDebug")
elseif(CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreadedDebug")
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DKEYSTONE_BUILD_STATIC_RUNTIME=ON)
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -D${LLVM_USE_CRT_VAR}=MTd)
elseif(${CMAKE_MSVC_RUNTIME_LIBRARY} STREQUAL "MultiThreadedDLL")
elseif(CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreadedDLL")
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DKEYSTONE_BUILD_STATIC_RUNTIME=OFF)
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -D${LLVM_USE_CRT_VAR}=MD)
elseif(${CMAKE_MSVC_RUNTIME_LIBRARY} STREQUAL "MultiThreadedDebugDLL")
elseif(CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreadedDebugDLL")
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DKEYSTONE_BUILD_STATIC_RUNTIME=OFF)
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -D${LLVM_USE_CRT_VAR}=MDd)
else()
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DKEYSTONE_BUILD_STATIC_RUNTIME=OFF)
endif()
else ()
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DKEYSTONE_BUILD_STATIC_RUNTIME=OFF)
endif()
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
set(KEYSTONE_CMAKE_ARGS ${KEYSTONE_CMAKE_ARGS} -DLLVM_TARGETS_TO_BUILD=AArch64)
Expand Down

0 comments on commit 7541d5e

Please sign in to comment.