Skip to content

Commit 53b0e05

Browse files
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.
1 parent 53f12f9 commit 53b0e05

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

BackwardConfig.cmake

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,27 @@ if(WIN32)
209209
if(SUPPORT_WINDOWS_DEBUG_INFO)
210210
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--pdb= ")
211211
add_compile_options(-gcodeview)
212-
else()
213-
find_library(MINGW_MSVCR_LIBRARY ucrtbase msvcr140 msvcr120 msvcr110 msvcr100 msvcr90 REQUIRED DOC "Mingw MSVC runtime import library")
214-
list(APPEND _BACKWARD_LIBRARIES ${MINGW_MSVCR_LIBRARY})
215212
endif()
216-
endif()
213+
# Look for _set_abort_behavior in a few msvcr implementations starting from most recent one
214+
include(CheckCXXSourceCompiles)
215+
include(CMakePushCheckState)
216+
set(src_set_abort_behavior "#include <stdlib.h>\nint main(int argc, char* argv[]) { _set_abort_behavior(0, 0); return 0;}\n")
217+
set(_MSVCR_LIBRARIES ucrtbase msvcr140 msvcr120 msvcr110 msvcr100 msvcr90)
218+
foreach(_MSVCR_LIBRARY ${_MSVCR_LIBRARIES})
219+
cmake_push_check_state(RESET)
220+
set(CMAKE_REQUIRED_LIBRARIES ${_MSVCR_LIBRARY})
221+
check_cxx_source_compiles("${src_set_abort_behavior}" _MSVCR_FOUND_LIBRARY)
222+
if(_MSVCR_FOUND_LIBRARY)
223+
set(MINGW_MSVCR_LIBRARY ${_MSVCR_LIBRARY} CACHE STRING "Mingw MSVC runtime import library")
224+
list(APPEND _BACKWARD_LIBRARIES ${MINGW_MSVCR_LIBRARY})
225+
break()
226+
endif()
227+
cmake_pop_check_state()
228+
endforeach()
229+
if(NOT _MSVCR_FOUND_LIBRARY)
230+
message(FATAL_ERROR "At least one of the import libraries ${_MSVCR_LIBRARIES} should be available ")
231+
endif()
232+
endif()
217233
endif()
218234

219235
set(BACKWARD_INCLUDE_DIR

0 commit comments

Comments
 (0)