From 53b0e05d4b1e90ef50c47f56b567cbb287b01d76 Mon Sep 17 00:00:00 2001 From: Giuseppe Corbelli Date: Fri, 26 Jan 2024 15:39:49 +0100 Subject: [PATCH] Link with ucrtbase/msvcr* available on the system Use check_cxx_source_compiles() to find the most recent linkable ucrtbase/msvcr. Linking of ucrtbase/msvcr is enabled regardless of the -gcodeview support, as the two concepts do not overlap. --- BackwardConfig.cmake | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/BackwardConfig.cmake b/BackwardConfig.cmake index e66533e..995b17a 100644 --- a/BackwardConfig.cmake +++ b/BackwardConfig.cmake @@ -209,11 +209,27 @@ if(WIN32) if(SUPPORT_WINDOWS_DEBUG_INFO) set(CMAKE_EXE_LINKER_FLAGS "-Wl,--pdb= ") add_compile_options(-gcodeview) - else() - find_library(MINGW_MSVCR_LIBRARY ucrtbase msvcr140 msvcr120 msvcr110 msvcr100 msvcr90 REQUIRED DOC "Mingw MSVC runtime import library") - list(APPEND _BACKWARD_LIBRARIES ${MINGW_MSVCR_LIBRARY}) endif() - endif() + # Look for _set_abort_behavior in a few msvcr implementations starting from most recent one + include(CheckCXXSourceCompiles) + include(CMakePushCheckState) + set(src_set_abort_behavior "#include \nint main(int argc, char* argv[]) { _set_abort_behavior(0, 0); return 0;}\n") + set(_MSVCR_LIBRARIES ucrtbase msvcr140 msvcr120 msvcr110 msvcr100 msvcr90) + foreach(_MSVCR_LIBRARY ${_MSVCR_LIBRARIES}) + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_LIBRARIES ${_MSVCR_LIBRARY}) + check_cxx_source_compiles("${src_set_abort_behavior}" _MSVCR_FOUND_LIBRARY) + if(_MSVCR_FOUND_LIBRARY) + set(MINGW_MSVCR_LIBRARY ${_MSVCR_LIBRARY} CACHE STRING "Mingw MSVC runtime import library") + list(APPEND _BACKWARD_LIBRARIES ${MINGW_MSVCR_LIBRARY}) + break() + endif() + cmake_pop_check_state() + endforeach() + if(NOT _MSVCR_FOUND_LIBRARY) + message(FATAL_ERROR "At least one of the import libraries ${_MSVCR_LIBRARIES} should be available ") + endif() + endif() endif() set(BACKWARD_INCLUDE_DIR