Skip to content

Commit

Permalink
CMake: add MSVC exception handling flags and preprocessor enabling flag.
Browse files Browse the repository at this point in the history
Suppress warning C5105 caused by adding the preprocessor flag.
Without these fixes the libm2k.dll built with MSVC did not throw any
exceptions, causing undefined behaviour throughout the library.

Signed-off-by: AlexandraTrifan <[email protected]>
  • Loading branch information
adisuciu authored and AlexandraTrifan committed Sep 1, 2022
1 parent 3cca459 commit 3e40d98
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ endif()

#Enable or disable exception handling
if (NOT ENABLE_EXCEPTIONS)
remove_definitions("-fexceptions")
add_definitions("-fno-exceptions")
add_definitions(-D_EXCEPTIONS=0)
if(NOT MSVC)
remove_definitions("-fexceptions")
add_definitions("-fno-exceptions")
endif()
else()
remove_definitions("-fno-exceptions")
add_definitions("-fexceptions")
add_definitions(-D_EXCEPTIONS=1)
if(NOT MSVC)
remove_definitions("-fno-exceptions")
add_definitions("-fexceptions")
endif()
endif()

configure_file(${CMAKE_SOURCE_DIR}/version.hpp.cmakein ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/version.hpp @ONLY)
Expand Down
21 changes: 20 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ if(APPLE)
set(CMAKE_OSX_ARCHITECTURES "x86_64")
endif()

if(MSVC)
if (NOT ENABLE_EXCEPTIONS)
# support for C/C++ conformant preprocessor in MSVC was added starting with
# version Visual Studio 2019 version 16.6 (equivalent to 1926)
# https://devblogs.microsoft.com/cppblog/announcing-full-support-for-a-c-c-conformant-preprocessor-in-msvc/
if(MSVC_VERSION GREATER_EQUAL 1926)
remove_definitions("/EHsc /Zc:preprocessor")
else()
remove_definitions("/EHsc /experimental:preprocessor")
endif()
else()
if(MSVC_VERSION GREATER_EQUAL 1926)
add_definitions("/permissive /EHsc /Zc:preprocessor")
else()
add_definitions("/permissive /EHsc /experimental:preprocessor")
endif()
endif()
endif()

SET(CMAKE_EXE_LINKER_FLAGS "/FORCE")

FILE(GLOB_RECURSE SRC_LIST *.cpp *_impl.hpp */*_impl.hpp)
Expand Down Expand Up @@ -38,7 +57,7 @@ if (ENABLE_LOG)
target_compile_definitions(${PROJECT_NAME} PUBLIC LIBM2K_ENABLE_LOG)
endif()
if (MSVC)
add_definitions("/wd4250 /wd4251 /wd4275")
add_definitions("/MD /wd4250 /wd4251 /wd4275 /wd5105")
endif()

if(NOT MSVC)
Expand Down

0 comments on commit 3e40d98

Please sign in to comment.