check all link libraries (pthread, rt, and atomic) via check_linker_flag first before using find_library (see also issue #898

This commit is contained in:
Daan 2024-05-19 15:38:26 -07:00
parent 03224e17a0
commit f739e4f2c3

View File

@ -340,25 +340,40 @@ if (MSVC AND MSVC_VERSION GREATER_EQUAL 1914)
endif()
# extra needed libraries
# we prefer -l<lib> test over `find_library` as sometimes core libraries
# like `libatomic` are not on the system path (see issue #898)
function(add_link_library libname outlibname)
check_linker_flag(C "-l${libname}" mi_has_lib${libname})
if (mi_has_lib${libname})
message(VERBOSE "link library: -l${libname}")
set(${outlibname} ${libname} PARENT_SCOPE)
else()
find_library(MI_LIBPATH libname)
if (MI_LIBPATH)
message(VERBOSE "link library ${libname} at ${MI_LIBPATH}")
set(${outlibname} ${MI_LIBPATH} PARENT_SCOPE)
else()
message(VERBOSE "link library not found: ${libname}")
set(${outlibname} "" PARENT_SCOPE)
endif()
endif()
endfunction()
if(WIN32)
list(APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt)
set(pc_libraries "-lpsapi -lshell32 -luser32 -ladvapi32 -lbcrypt")
list(APPEND mi_libraries psapi shell32 user32 advapi32 bcrypt)
else()
set(pc_libraries "")
find_library(MI_LIBPTHREAD pthread)
if (MI_LIBPTHREAD)
list(APPEND mi_libraries ${MI_LIBPTHREAD})
set(pc_libraries "${pc_libraries} -pthread")
add_link_library("pthread" MI_LIB_PTHREAD)
if(MI_LIB_PTHREAD)
list(APPEND mi_libraries "${MI_LIB_PTHREAD}")
endif()
find_library(MI_LIBRT rt)
if(MI_LIBRT)
list(APPEND mi_libraries ${MI_LIBRT})
set(pc_libraries "${pc_libraries} -lrt")
add_link_library("rt" MI_LIB_RT)
if(MI_LIB_RT)
list(APPEND mi_libraries "${MI_LIB_RT}")
endif()
check_linker_flag(C "-latomic" MI_HAS_LIBATOMIC) # do not use `find_library` as libatomic is not always in the system path (issue #898)
if (MI_HAS_LIBATOMIC OR MI_USE_LIBATOMIC)
list(APPEND mi_libraries "atomic")
set(pc_libraries "${pc_libraries} -latomic")
add_link_library("atomic" MI_LIB_ATOMIC)
if(MI_LIB_ATOMIC)
list(APPEND mi_libraries "${MI_LIB_ATOMIC}")
endif()
endif()
@ -520,6 +535,15 @@ if (MI_BUILD_OBJECT)
endif()
# pkg-config file support
set(pc_libraries "")
foreach(item IN LISTS mi_libraries)
if(item MATCHES " *[-].*")
set(pc_libraries "${pc_libraries} ${item}")
else()
set(pc_libraries "${pc_libraries} -l${item}")
endif()
endforeach()
include("cmake/JoinPaths.cmake")
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
join_paths(libdir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_LIBDIR}")
@ -528,6 +552,8 @@ configure_file(mimalloc.pc.in mimalloc.pc @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/mimalloc.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/")
# -----------------------------------------------------------------------------
# API surface testing
# -----------------------------------------------------------------------------