refactoring find packages

This commit is contained in:
Jérôme BOULMIER 2019-06-25 01:21:31 -04:00 committed by Patrick Ziegler
parent b143e43491
commit cb0aeacbf2
8 changed files with 60 additions and 155 deletions

View File

@ -71,31 +71,47 @@ function(queryfont output_variable fontname)
endfunction() endfunction()
# }}} # }}}
# querylib {{{ # find_package_impl {{{
function(querylib flag type pkg out_library out_include_dirs) macro(find_package_impl pkg_config_name find_pkg_name header_to_find)
if(${flag})
if(${type} STREQUAL "cmake")
find_package(${pkg} REQUIRED)
string(TOUPPER ${pkg} pkg_upper)
list(APPEND ${out_library} ${${pkg_upper}_LIBRARY})
list(APPEND ${out_include_dirs} ${${pkg_upper}_INCLUDE_DIR})
elseif(${type} STREQUAL "pkg-config")
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_check_modules(PKG_${flag} REQUIRED ${pkg}) include(FindPackageHandleStandardArgs)
# Set packet version so that it can be used in the summary pkg_check_modules(PC_${find_pkg_name} REQUIRED ${pkg_config_name})
set(${flag}_VERSION ${PKG_${flag}_VERSION} PARENT_SCOPE)
list(APPEND ${out_library} ${PKG_${flag}_LIBRARIES}) if (NOT ${header_to_find} STREQUAL "")
list(APPEND ${out_include_dirs} ${PKG_${flag}_INCLUDE_DIRS}) find_path(PC_${find_pkg_name}_INCLUDE_DIRS_
else() NAMES "${header_to_find}"
message(FATAL_ERROR "Invalid lookup type '${type}'") HINTS "${PC_${find_pkg_name}_INCLUDE_DIRS}"
endif() )
set(${out_library} ${${out_library}} PARENT_SCOPE) set(PC_${find_pkg_name}_INCLUDE_DIRS ${PC_${find_pkg_name}_INCLUDE_DIRS_})
set(${out_include_dirs} ${${out_include_dirs}} PARENT_SCOPE)
endif() endif()
set(${find_pkg_name}_INCLUDE_DIR ${PC_${find_pkg_name}_INCLUDE_DIRS})
set(${find_pkg_name}_INCLUDE_DIRS ${${find_pkg_name}_INCLUDE_DIR})
set(${find_pkg_name}_LIBRARY ${PC_${find_pkg_name}_LIBRARIES})
set(${find_pkg_name}_LIBRARIES ${${find_pkg_name}_LIBRARY})
find_package_handle_standard_args(${find_pkg_name}
REQUIRED_VARS
${find_pkg_name}_INCLUDE_DIRS
${find_pkg_name}_LIBRARIES
VERSION_VAR
PC_${find_pkg_name}_VERSION
)
mark_as_advanced(${find_pkg_name}_INCLUDE_DIR ${find_pkg_name}_LIBRARY)
endmacro()
# }}}
# create_imported_target {{{
function(create_imported_target library_name includes libraries)
add_library(${library_name} INTERFACE IMPORTED)
set_target_properties(${library_name} PROPERTIES
INTERFACE_LINK_LIBRARIES "${libraries}"
)
target_include_directories(${library_name} SYSTEM INTERFACE ${includes})
endfunction() endfunction()
# }}} # }}}
# checklib {{{ # checklib {{{

View File

@ -1,31 +1,5 @@
find_package(PkgConfig REQUIRED) find_package_impl("cairo-fc" "CairoFC" "")
include(CMakeFindDependencyMacro)
pkg_check_modules(PC_CairoFC QUIET cairo-fc)
set(CairoFC_INCLUDE_DIR ${PC_CairoFC_INCLUDE_DIRS})
set(CairoFC_LIBRARY ${PC_CairoFC_LIBRARIES})
include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set CairoFC_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(CairoFC
REQUIRED_VARS
CairoFC_INCLUDE_DIR
CairoFC_LIBRARY
VERSION_VAR
PC_CairoFC_VERSION)
message(STATUS "${CairoFC}")
mark_as_advanced(CairoFC_INCLUDE_DIR CairoFC_LIBRARY)
set(CairoFC_LIBRARIES ${CairoFC_LIBRARY})
set(CairoFC_INCLUDE_DIRS ${CairoFC_INCLUDE_DIR})
if(CairoFC_FOUND AND NOT TARGET Cairo::CairoFC) if(CairoFC_FOUND AND NOT TARGET Cairo::CairoFC)
add_library(Cairo::CairoFC INTERFACE IMPORTED) create_imported_target("Cairo::CairoFC" "${CairoFC_INCLUDE_DIR}" "${CairoFC_LIBRARY}")
set_target_properties(Cairo::CairoFC PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${CairoFC_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${CairoFC_LIBRARIES}"
)
endif() endif()

View File

@ -1,26 +1,5 @@
function(libinotify) find_package_impl("libinotify" "LibInotify" "")
find_package(PkgConfig REQUIRED)
pkg_check_modules(PC_LibInotify QUIET libinotify)
include(FindPackageHandleStandardArgs) if(LibInotify_FOUND AND NOT TARGET LibInotify::LibInotify)
create_imported_target("LibInotify::LibInotify" "${LibInotify_INCLUDE_DIR}" "${LibInotify_LIBRARY}")
find_path(LibInotify_INCLUDES endif()
NAMES netlink/version.h
HINTS ${PC_LibInotify_INCLUDEDIR} ${PC_LibInotify_INCLUDE_DIRS}
)
find_package_handle_standard_args(LibInotify
REQUIRED_VARS PC_LibInotify_INCLUDE_DIRS
VERSION_VAR PC_LibInotify_VERSION
)
if(LibInotify_FOUND AND NOT TARGET LibInotify::LibInotify)
add_library(LibInotify::LibInotify INTERFACE IMPORTED)
set_target_properties(LibInotify::LibInotify PROPERTIES
INTERFACE_LINK_LIBRARIES "${PC_LibInotify_LIBRARIES}")
target_include_directories(LibInotify::LibInotify SYSTEM INTERFACE ${LibInotify_INCLUDES})
endif()
endfunction()
libinotify()

View File

@ -1,22 +1,5 @@
find_package(PkgConfig REQUIRED) find_package_impl("libmpdclient" "LibMPDClient" "mpd/player.h")
pkg_check_modules(PC_LibMPDClient QUIET libmpdclient)
include(FindPackageHandleStandardArgs)
find_path(LibMPDClient_INCLUDES
NAMES mpd/player.h
HINTS ${PC_LibMPDClient_INCLUDEDIR} ${PC_LibMPDClient_INCLUDE_DIRS}
)
find_package_handle_standard_args(LibMPDClient
REQUIRED_VARS LibMPDClient_INCLUDES
VERSION_VAR PC_LibMPDClient_VERSION
)
if(LibMPDClient_FOUND AND NOT TARGET LibMPDClient::LibMPDClient) if(LibMPDClient_FOUND AND NOT TARGET LibMPDClient::LibMPDClient)
add_library(LibMPDClient::LibMPDClient INTERFACE IMPORTED) create_imported_target("LibMPDClient::LibMPDClient" "${LibMPDClient_INCLUDE_DIR}" "${LibMPDClient_LIBRARY}")
set_target_properties(LibMPDClient::LibMPDClient PROPERTIES
INTERFACE_LINK_LIBRARIES "${PC_LibMPDClient_LIBRARIES}")
target_include_directories(LibMPDClient::LibMPDClient SYSTEM INTERFACE ${LibMPDClient_INCLUDES})
endif() endif()

View File

@ -1,17 +1,5 @@
find_package(PkgConfig REQUIRED) find_package_impl("libnl-genl-3.0" "LibNlGenl3" "")
pkg_check_modules(PC_LibNlGenl3 QUIET libnl-genl-3.0)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LibNlGenl3
REQUIRED_VARS PC_LibNlGenl3_INCLUDE_DIRS
VERSION_VAR PC_LibNlGenl3_VERSION
)
if(LibNlGenl3_FOUND AND NOT TARGET LibNlGenl3::LibNlGenl3) if(LibNlGenl3_FOUND AND NOT TARGET LibNlGenl3::LibNlGenl3)
add_library(LibNlGenl3::LibNlGenl3 INTERFACE IMPORTED) create_imported_target("LibNlGenl3::LibNlGenl3" "${LibNlGenl3_INCLUDE_DIR}" "${LibNlGenl3_LIBRARY}")
set_target_properties(LibNlGenl3::LibNlGenl3 PROPERTIES
INTERFACE_LINK_LIBRARIES "${PC_LibNlGenl3_LIBRARIES}")
target_include_directories(LibNlGenl3::LibNlGenl3 SYSTEM INTERFACE ${PC_LibNlGenl3_INCLUDE_DIRS})
endif() endif()

View File

@ -1,23 +1,5 @@
find_package_impl("libpulse" "LibPulse" "pulse/version.h")
find_package(PkgConfig REQUIRED)
pkg_check_modules(PC_LibPulse QUIET libpulse)
include(FindPackageHandleStandardArgs)
find_path(LibPulse_INCLUDES
NAMES pulse/version.h
HINTS ${PC_LibPulse_INCLUDEDIR} ${PC_LibPulse_INCLUDE_DIRS}
)
find_package_handle_standard_args(LibPulse
REQUIRED_VARS LibPulse_INCLUDES
VERSION_VAR PC_LibPulse_VERSION
)
if(LibPulse_FOUND AND NOT TARGET LibPulse::LibPulse) if(LibPulse_FOUND AND NOT TARGET LibPulse::LibPulse)
add_library(LibPulse::LibPulse INTERFACE IMPORTED) create_imported_target("LibPulse::LibPulse" "${LibPulse_INCLUDE_DIR}" "${LibPulse_LIBRARY}")
set_target_properties(LibPulse::LibPulse PROPERTIES
INTERFACE_LINK_LIBRARIES "${PC_LibPulse_LIBRARIES}")
target_include_directories(LibPulse::LibPulse SYSTEM INTERFACE ${LibPulse_INCLUDES})
endif() endif()

View File

@ -17,8 +17,5 @@ find_package_handle_standard_args(Libiw DEFAULT_MSG LIBIW_LIBRARY LIBIW_INCLUDE_
mark_as_advanced(LIBIW_INCLUDE_DIR LIBIW_LIBRARY) mark_as_advanced(LIBIW_INCLUDE_DIR LIBIW_LIBRARY)
if(Libiw_FOUND AND NOT TARGET Libiw::Libiw) if(Libiw_FOUND AND NOT TARGET Libiw::Libiw)
add_library(Libiw::Libiw INTERFACE IMPORTED) create_imported_target("Libiw::Libiw" "${LIBIW_INCLUDE_DIR}" "${LIBIW_LIBRARIES}")
set_target_properties(Libiw::Libiw PROPERTIES
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${LIBIW_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${LIBIW_LIBRARY}")
endif() endif()

View File

@ -1,6 +1,4 @@
cmake_policy(PUSH) # This script only supports the following components of XCB
cmake_policy(SET CMP0057 NEW)
set(XCB_known_components set(XCB_known_components
XCB XCB
RANDR RANDR
@ -9,44 +7,32 @@ set(XCB_known_components
XRM XRM
CURSOR) CURSOR)
# Deducing header from the name of the component
foreach(_comp ${XCB_known_components}) foreach(_comp ${XCB_known_components})
string(TOLOWER "${_comp}" _lc_comp) string(TOLOWER "${_comp}" _lc_comp)
set(XCB_${_comp}_pkg_config "xcb-${_lc_comp}") set(XCB_${_comp}_pkg_config "xcb-${_lc_comp}")
set(XCB_${_comp}_header "xcb/${_lc_comp}.h") set(XCB_${_comp}_header "xcb/${_lc_comp}.h")
endforeach() endforeach()
# Exception cases
set(XCB_XRM_header "xcb/xcb_xrm.h") set(XCB_XRM_header "xcb/xcb_xrm.h")
set(XCB_CURSOR_header "xcb/xcb_cursor.h") set(XCB_CURSOR_header "xcb/xcb_cursor.h")
find_package(PkgConfig REQUIRED) cmake_policy(PUSH)
include(FindPackageHandleStandardArgs) cmake_policy(SET CMP0057 NEW)
foreach(_comp ${Xcb_FIND_COMPONENTS}) foreach(_comp ${Xcb_FIND_COMPONENTS})
if (NOT ${_comp} IN_LIST XCB_known_components) if (NOT ${_comp} IN_LIST XCB_known_components)
cmake_policy(POP) cmake_policy(POP)
message(FATAL_ERROR "Unknow component ${_comp} for XCB") message(FATAL_ERROR "Unknow component \"${_comp}\" of XCB")
endif() endif()
pkg_check_modules(PC_${_comp} QUIET ${XCB_${_comp}_pkg_config}) find_package_impl(${XCB_${_comp}_pkg_config} "Xcb_${_comp}" "${XCB_${_comp}_header}")
find_path(${_comp}_INCLUDES_DIRS
NAMES "${XCB_${_comp}_header}"
HINTS ${PC_${_comp}_INCLUDEDIR} ${PC_${_comp}_INCLUDE_DIRS}
)
find_package_handle_standard_args(Xcb_${_comp}
REQUIRED_VARS ${_comp}_INCLUDES_DIRS
VERSION_VAR PC_${_comp}_VERSION
HANDLE_COMPONENTS
)
if(Xcb_${_comp}_FOUND AND NOT TARGET Xcb::${_comp}) if(Xcb_${_comp}_FOUND AND NOT TARGET Xcb::${_comp})
add_library(Xcb::${_comp} INTERFACE IMPORTED) create_imported_target("Xcb::${_comp}" "${Xcb_${_comp}_INCLUDE_DIRS}" "${Xcb_${_comp}_LIBRARIES}")
set_target_properties(Xcb::${_comp} PROPERTIES
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${_comp}_INCLUDES"
INTERFACE_LINK_LIBRARIES "${PC_${_comp}_LIBRARIES}"
)
elseif(NOT Xcb_${_comp}_FOUND AND Xcb_FIND_REQUIRED) elseif(NOT Xcb_${_comp}_FOUND AND Xcb_FIND_REQUIRED)
message(FATAL_ERROR "Xcb: Required component \"${component}\" is not found") cmake_policy(POP)
message(FATAL_ERROR "Xcb: Required component \"${_comp}\" is not found")
endif() endif()
endforeach() endforeach()