cmake: fix version checks for external packages

The name passed to find_package should match the one that is passed to
find_package_handle_standard_args, otherwise the version matching is not
properly done. The names are based on the FindXXX.cmake filename.

This also simplifies components reporting for Libevent (done by
find_package_handle_standard_args) and results in reporting a library
instead of include directory when found.
This commit is contained in:
Peter Wu 2016-03-13 13:09:37 +01:00
parent a45540fd79
commit 2b3bc710fc
3 changed files with 16 additions and 22 deletions

View File

@ -28,7 +28,7 @@ endif()
include(FindPackageHandleStandardArgs) include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set JANSSON_FOUND to TRUE # handle the QUIETLY and REQUIRED arguments and set JANSSON_FOUND to TRUE
# if all listed variables are TRUE and the requested version matches. # if all listed variables are TRUE and the requested version matches.
find_package_handle_standard_args(jansson REQUIRED_VARS find_package_handle_standard_args(Jansson REQUIRED_VARS
JANSSON_LIBRARY JANSSON_INCLUDE_DIR JANSSON_LIBRARY JANSSON_INCLUDE_DIR
VERSION_VAR JANSSON_VERSION) VERSION_VAR JANSSON_VERSION)

View File

@ -28,7 +28,7 @@ endif()
include(FindPackageHandleStandardArgs) include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set JEMALLOC_FOUND to TRUE # handle the QUIETLY and REQUIRED arguments and set JEMALLOC_FOUND to TRUE
# if all listed variables are TRUE and the requested version matches. # if all listed variables are TRUE and the requested version matches.
find_package_handle_standard_args(jemalloc REQUIRED_VARS find_package_handle_standard_args(Jemalloc REQUIRED_VARS
JEMALLOC_LIBRARY JEMALLOC_INCLUDE_DIR JEMALLOC_LIBRARY JEMALLOC_INCLUDE_DIR
VERSION_VAR JEMALLOC_VERSION) VERSION_VAR JEMALLOC_VERSION)

View File

@ -20,7 +20,7 @@
# LIBEVENT_INCLUDE_DIRS - Libevent include directories # LIBEVENT_INCLUDE_DIRS - Libevent include directories
# LIBEVENT_LIBRARIES - Libevent libraries to be linked # LIBEVENT_LIBRARIES - Libevent libraries to be linked
# LIBEVENT_<C>_FOUND - Component <C> was found (<C> is uppercase) # LIBEVENT_<C>_FOUND - Component <C> was found (<C> is uppercase)
# LIBEVENT_<C>_LIBRARY - Library to be linked for Libevent omponent <C>. # LIBEVENT_<C>_LIBRARY - Library to be linked for Libevent component <C>.
find_package(PkgConfig QUIET) find_package(PkgConfig QUIET)
pkg_check_modules(PC_LIBEVENT QUIET libevent) pkg_check_modules(PC_LIBEVENT QUIET libevent)
@ -33,7 +33,6 @@ find_path(LIBEVENT_INCLUDE_DIR
HINTS HINTS
${PC_LIBEVENT_INCLUDE_DIRS} ${PC_LIBEVENT_INCLUDE_DIRS}
) )
set(_LIBEVENT_REQUIRED_VARS LIBEVENT_INCLUDE_DIR)
if(LIBEVENT_INCLUDE_DIR) if(LIBEVENT_INCLUDE_DIR)
set(_version_regex "^#define[ \t]+_EVENT_VERSION[ \t]+\"([^\"]+)\".*") set(_version_regex "^#define[ \t]+_EVENT_VERSION[ \t]+\"([^\"]+)\".*")
@ -51,6 +50,7 @@ if(LIBEVENT_INCLUDE_DIR)
unset(_version_regex) unset(_version_regex)
endif() endif()
set(_LIBEVENT_REQUIRED_VARS)
foreach(COMPONENT ${Libevent_FIND_COMPONENTS}) foreach(COMPONENT ${Libevent_FIND_COMPONENTS})
set(_LIBEVENT_LIBNAME libevent) set(_LIBEVENT_LIBNAME libevent)
# Note: compare two variables to avoid a CMP0054 policy warning # Note: compare two variables to avoid a CMP0054 policy warning
@ -59,42 +59,36 @@ foreach(COMPONENT ${Libevent_FIND_COMPONENTS})
else() else()
set(_LIBEVENT_LIBNAME "event_${COMPONENT}") set(_LIBEVENT_LIBNAME "event_${COMPONENT}")
endif() endif()
string(TOUPPER "${COMPONENT}" COMPONENT) string(TOUPPER "${COMPONENT}" COMPONENT_UPPER)
find_library(LIBEVENT_${COMPONENT}_LIBRARY find_library(LIBEVENT_${COMPONENT_UPPER}_LIBRARY
NAMES ${_LIBEVENT_LIBNAME} NAMES ${_LIBEVENT_LIBNAME}
HINTS ${PC_LIBEVENT_LIBRARY_DIRS} HINTS ${PC_LIBEVENT_LIBRARY_DIRS}
) )
if(LIBEVENT_${COMPONENT}_LIBRARY) if(LIBEVENT_${COMPONENT_UPPER}_LIBRARY)
set(LIBEVENT_${COMPONENT}_FOUND 1) set(Libevent_${COMPONENT}_FOUND 1)
endif() endif()
list(APPEND _LIBEVENT_REQUIRED_VARS LIBEVENT_${COMPONENT}_LIBRARY) list(APPEND _LIBEVENT_REQUIRED_VARS LIBEVENT_${COMPONENT_UPPER}_LIBRARY)
endforeach() endforeach()
unset(_LIBEVENT_LIBNAME) unset(_LIBEVENT_LIBNAME)
include(FindPackageHandleStandardArgs) include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LIBEVENT_FOUND to TRUE # handle the QUIETLY and REQUIRED arguments and set LIBEVENT_FOUND to TRUE
# if all listed variables are TRUE and the requested version matches. # if all listed variables are TRUE and the requested version matches.
find_package_handle_standard_args(LIBEVENT REQUIRED_VARS find_package_handle_standard_args(Libevent REQUIRED_VARS
${_LIBEVENT_REQUIRED_VARS} ${_LIBEVENT_REQUIRED_VARS}
LIBEVENT_INCLUDE_DIR
VERSION_VAR LIBEVENT_VERSION VERSION_VAR LIBEVENT_VERSION
HANDLE_COMPONENTS) HANDLE_COMPONENTS)
if(LIBEVENT_FOUND) if(LIBEVENT_FOUND)
set(LIBEVENT_INCLUDE_DIRS ${LIBEVENT_INCLUDE_DIR}) set(LIBEVENT_INCLUDE_DIRS ${LIBEVENT_INCLUDE_DIR})
set(LIBEVENT_LIBRARIES) set(LIBEVENT_LIBRARIES)
if(NOT Libevent_FIND_QUIETLY) foreach(COMPONENT ${Libevent_FIND_COMPONENTS})
message(STATUS "Found the following Libevent components:") string(TOUPPER "${COMPONENT}" COMPONENT_UPPER)
endif() list(APPEND LIBEVENT_LIBRARIES ${LIBEVENT_${COMPONENT_UPPER}_LIBRARY})
foreach(_COMPONENT ${Libevent_FIND_COMPONENTS}) set(LIBEVENT_${COMPONENT_UPPER}_FOUND ${Libevent_${COMPONENT}_FOUND})
string(TOUPPER "${_COMPONENT}" COMPONENT)
if(LIBEVENT_${COMPONENT}_FOUND)
if(NOT Libevent_FIND_QUIETLY)
message(STATUS " ${_COMPONENT}")
endif()
list(APPEND LIBEVENT_LIBRARIES ${LIBEVENT_${COMPONENT}_LIBRARY})
endif()
endforeach() endforeach()
endif() endif()
mark_as_advanced(${_LIBEVENT_REQUIRED_VARS}) mark_as_advanced(LIBEVENT_INCLUDE_DIR ${_LIBEVENT_REQUIRED_VARS})
unset(_LIBEVENT_REQUIRED_VARS) unset(_LIBEVENT_REQUIRED_VARS)