mdbx-cmake: pthread workaround for buggy toolchain/cmake/buildroot.

Change-Id: I0d95e783abbd10a63cd1595a9de50593e814a967
This commit is contained in:
Leonid Yuriev 2020-10-05 19:12:20 +03:00
parent 8d4e7994c0
commit 787eaaa373

View File

@ -173,10 +173,27 @@ if(NOT CMAKE_BUILD_TYPE)
endif() endif()
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPERCASE) string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPERCASE)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
include(cmake/utils.cmake) include(cmake/utils.cmake)
include(cmake/compiler.cmake) include(cmake/compiler.cmake)
include(cmake/profile.cmake) include(cmake/profile.cmake)
# Workaround for `-pthread` toolchain/cmake bug
if(NOT APPLE AND NOT MSVC
AND CMAKE_USE_PTHREADS_INIT AND NOT CMAKE_THREAD_LIBS_INIT
AND (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG))
check_compiler_flag("-pthread" CC_HAS_PTHREAD)
if(CC_HAS_PTHREAD AND NOT CMAKE_EXE_LINKER_FLAGS MATCHES "-pthread")
message(STATUS "Force add -pthread for linker flags to avoid troubles")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pthread")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -pthread")
endif()
endif()
CHECK_FUNCTION_EXISTS(pow NOT_NEED_LIBM) CHECK_FUNCTION_EXISTS(pow NOT_NEED_LIBM)
if(NOT_NEED_LIBM) if(NOT_NEED_LIBM)
set(LIB_MATH "") set(LIB_MATH "")
@ -190,8 +207,6 @@ else()
endif() endif()
endif() endif()
find_package(Threads REQUIRED)
if(SUBPROJECT) if(SUBPROJECT)
if(NOT DEFINED BUILD_SHARED_LIBS) if(NOT DEFINED BUILD_SHARED_LIBS)
option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)" OFF) option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)" OFF)
@ -541,7 +556,7 @@ macro(target_setup_options TARGET)
endmacro() endmacro()
macro(libmdbx_setup_libs TARGET MODE) macro(libmdbx_setup_libs TARGET MODE)
target_link_libraries(${TARGET} ${MODE} ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(${TARGET} ${MODE} Threads::Threads)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
target_link_libraries(${TARGET} ${MODE} ntdll.lib) target_link_libraries(${TARGET} ${MODE} ntdll.lib)
if(MDBX_NTDLL_EXTRA_IMPLIB AND MDBX_AVOID_CRT) if(MDBX_NTDLL_EXTRA_IMPLIB AND MDBX_AVOID_CRT)