CMake: Rework library function checking (#310)

CHECK_LIBRARY_EXISTS(), while it takes a function name, does not actually verify the function exists in the library being evaluated. Instead, if the function is found in any dependent library, and the referenced library also exists, it returns true. This is not desirable.

Wrap with a Macro to change the behavior.

Fixes: #307
Fix By: Brad House (@bradh352)
pull/312/head
Brad House 5 years ago committed by GitHub
parent e0517f97d9
commit 65d0878870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      CMakeLists.txt

@ -72,8 +72,20 @@ SET (TARGETS_INST_DEST
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
# Function in Library
# CHECK_LIBRARY_EXISTS can't be used as it will return true if the function
# is found in a different dependent library.
MACRO (CARES_FUNCTION_IN_LIBRARY func lib var)
CHECK_FUNCTION_EXISTS ("${func}" "_CARES_FUNC_IN_LIB_GLOBAL_${func}")
IF ("${_CARES_FUNC_IN_LIB_GLOBAL_${func}}")
SET (${var} FALSE)
ELSE ()
CHECK_LIBRARY_EXISTS ("${lib}" "${func}" "" ${var})
ENDIF ()
ENDMACRO ()
# Look for dependent/required libraries
CHECK_LIBRARY_EXISTS (resolv res_servicename "" HAVE_RES_SERVICENAME_IN_LIBRESOLV)
CARES_FUNCTION_IN_LIBRARY (res_servicename resolv HAVE_RES_SERVICENAME_IN_LIBRESOLV)
IF (HAVE_RES_SERVICENAME_IN_LIBRESOLV)
SET (HAVE_LIBRESOLV 1)
ENDIF ()
@ -123,13 +135,14 @@ IF (IOS AND HAVE_LIBRESOLV)
SET (CARES_USE_LIBRESOLV 1)
ENDIF()
CHECK_LIBRARY_EXISTS (nsl gethostbyname "" HAVE_LIBNSL)
CHECK_LIBRARY_EXISTS (socket gethostbyname "" HAVE_GHBN_LIBSOCKET)
CHECK_LIBRARY_EXISTS (socket socket "" HAVE_SOCKET_LIBSOCKET)
CARES_FUNCTION_IN_LIBRARY (gethostbyname nsl HAVE_LIBNSL)
CARES_FUNCTION_IN_LIBRARY (gethostbyname socket HAVE_GHBN_LIBSOCKET)
CARES_FUNCTION_IN_LIBRARY (socket socket HAVE_SOCKET_LIBSOCKET)
IF (HAVE_GHBN_LIBSOCKET OR HAVE_SOCKET_LIBSOCKET)
SET(HAVE_LIBSOCKET TRUE)
ENDIF ()
CHECK_LIBRARY_EXISTS (rt clock_gettime "" HAVE_LIBRT)
CARES_FUNCTION_IN_LIBRARY (clock_gettime rt HAVE_LIBRT)
# Look for necessary includes

Loading…
Cancel
Save