CMake: Implement ADD_REQUIREMENTS_OF option for find_package2

Resolves #142
This commit is contained in:
Dennis Klein 2019-06-26 19:19:57 +02:00 committed by Dennis Klein
parent 3358a2ba12
commit 465d90924b

View File

@ -303,30 +303,74 @@ macro(install_cmake_package)
) )
endmacro() endmacro()
#
# find_package2(PRIVATE|PUBLIC|INTERFACE <pkgname>
# [VERSION <version>]
# [COMPONENTS <list of components>]
# [ADD_REQUIREMENTS_OF <list of dep_pgkname>]
# [any other option the native find_package supports]...)
#
# Wrapper around CMake's native find_package command to add some features and bookkeeping.
#
# The qualifier (PRIVATE|PUBLIC|INTERFACE) to the package to populate
# the variables PROJECT_[INTERFACE]_<pkgname>_([VERSION]|[COMPONENTS]|PACKAGE_DEPENDENCIES)
# accordingly. This bookkeeping information is used to print our dependency found summary
# table and to generate a part of our CMake package.
#
# When a dependending package is listed with ADD_REQUIREMENTS_OF the variables
# <dep_pkgname>_<pkgname>_VERSION|COMPONENTS are looked up to and added to the native
# VERSION (selected highest version) and COMPONENTS (deduplicated) args.
#
# COMPONENTS and VERSION args are then just passed to the native find_package.
#
macro(find_package2 qualifier pkgname) macro(find_package2 qualifier pkgname)
cmake_parse_arguments(ARGS "" "" "VERSION;COMPONENTS" ${ARGN}) cmake_parse_arguments(ARGS "" "" "VERSION;COMPONENTS;ADD_REQUIREMENTS_OF" ${ARGN})
string(TOUPPER ${pkgname} pkgname_upper) string(TOUPPER ${pkgname} pkgname_upper)
set(old_CPP ${CMAKE_PREFIX_PATH}) set(__old_cpp__ ${CMAKE_PREFIX_PATH})
set(CMAKE_PREFIX_PATH ${${pkgname_upper}_ROOT} $ENV{${pkgname_upper}_ROOT} ${CMAKE_PREFIX_PATH}) set(CMAKE_PREFIX_PATH ${${pkgname_upper}_ROOT} $ENV{${pkgname_upper}_ROOT} ${CMAKE_PREFIX_PATH})
unset(__version__)
# build lists of required versions and components
unset(__required_versions__)
unset(__components__)
if(ARGS_VERSION) if(ARGS_VERSION)
list(GET ARGS_VERSION 0 __version__) list(APPEND __required_versions__ ${ARGS_VERSION})
list(LENGTH ARGS_VERSION __length__) endif()
foreach(v IN LISTS ARGS_VERSION) if(ARGS_COMPONENTS)
list(APPEND __components__ ${ARGS_COMPONENTS})
endif()
if(ARGS_ADD_REQUIREMENTS_OF)
foreach(dep_pkgname IN LISTS ARGS_ADD_REQUIREMENTS_OF)
if(${dep_pkgname}_${pkgname}_VERSION)
list(APPEND __required_versions__ ${${dep_pkgname}_${pkgname}_VERSION})
endif()
if(${dep_pkgname}_${pkgname}_COMPONENTS)
list(APPEND __components__ ${${dep_pkgname}_${pkgname}_COMPONENTS})
endif()
endforeach()
endif()
# select highest required version
unset(__version__)
if(__required_versions__)
list(GET __required_versions__ 0 __version__)
foreach(v IN LISTS __required_versions__)
if(${v} VERSION_GREATER ${__version__}) if(${v} VERSION_GREATER ${__version__})
set(__version__ ${v}) set(__version__ ${v})
endif() endif()
endforeach() endforeach()
endif() endif()
if(ARGS_COMPONENTS) # deduplicate required component list
if(__components__)
list(REMOVE_DUPLICATES ARGS_COMPONENTS) list(REMOVE_DUPLICATES ARGS_COMPONENTS)
find_package(${pkgname} ${__version__} QUIET COMPONENTS ${ARGS_COMPONENTS} ${ARGS_UNPARSED_ARGUMENTS}) endif()
# call native find_package
if(__components__)
find_package(${pkgname} ${__version__} QUIET COMPONENTS ${__components__} ${ARGS_UNPARSED_ARGUMENTS})
else() else()
find_package(${pkgname} ${__version__} QUIET ${ARGS_UNPARSED_ARGUMENTS}) find_package(${pkgname} ${__version__} QUIET ${ARGS_UNPARSED_ARGUMENTS})
endif() endif()
set(CMAKE_PREFIX_PATH ${old_CPP})
unset(old_CPP)
if(${pkgname}_FOUND) if(${pkgname}_FOUND)
if(${qualifier} STREQUAL PRIVATE) if(${qualifier} STREQUAL PRIVATE)
@ -348,4 +392,8 @@ macro(find_package2 qualifier pkgname)
endif() endif()
unset(__version__) unset(__version__)
unset(__components__)
unset(__required_versions__)
set(CMAKE_PREFIX_PATH ${__old_cpp__})
unset(__old_cpp__)
endmacro() endmacro()