find_dependency: Always search dependencies

When a dependency was already found, find_dependency did not search it
again. While this works in basic case, it does not when there are
components as the check does not take components into account.

Given the fact that there is no documentation about this optimization and
that the correct implementation is not trivial as it would require
changes in find_package to have the list of components already found we
always search dependencies.

Fix #17583.
This commit is contained in:
Damien R
2019-03-29 00:47:23 +01:00
parent 14b6cd497f
commit 37da6af17d
5 changed files with 64 additions and 31 deletions

View File

@@ -31,35 +31,33 @@ CMakeFindDependencyMacro
#]=======================================================================]
macro(find_dependency dep)
if (NOT ${dep}_FOUND)
set(cmake_fd_quiet_arg)
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
set(cmake_fd_quiet_arg QUIET)
endif()
set(cmake_fd_required_arg)
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
set(cmake_fd_required_arg REQUIRED)
endif()
get_property(cmake_fd_alreadyTransitive GLOBAL PROPERTY
_CMAKE_${dep}_TRANSITIVE_DEPENDENCY
)
find_package(${dep} ${ARGN}
${cmake_fd_quiet_arg}
${cmake_fd_required_arg}
)
if(NOT DEFINED cmake_fd_alreadyTransitive OR cmake_fd_alreadyTransitive)
set_property(GLOBAL PROPERTY _CMAKE_${dep}_TRANSITIVE_DEPENDENCY TRUE)
endif()
if (NOT ${dep}_FOUND)
set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "${CMAKE_FIND_PACKAGE_NAME} could not be found because dependency ${dep} could not be found.")
set(${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
return()
endif()
set(cmake_fd_required_arg)
set(cmake_fd_quiet_arg)
set(cmake_fd_quiet_arg)
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
set(cmake_fd_quiet_arg QUIET)
endif()
set(cmake_fd_required_arg)
if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
set(cmake_fd_required_arg REQUIRED)
endif()
get_property(cmake_fd_alreadyTransitive GLOBAL PROPERTY
_CMAKE_${dep}_TRANSITIVE_DEPENDENCY
)
find_package(${dep} ${ARGN}
${cmake_fd_quiet_arg}
${cmake_fd_required_arg}
)
if(NOT DEFINED cmake_fd_alreadyTransitive OR cmake_fd_alreadyTransitive)
set_property(GLOBAL PROPERTY _CMAKE_${dep}_TRANSITIVE_DEPENDENCY TRUE)
endif()
if (NOT ${dep}_FOUND)
set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "${CMAKE_FIND_PACKAGE_NAME} could not be found because dependency ${dep} could not be found.")
set(${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
return()
endif()
set(cmake_fd_required_arg)
set(cmake_fd_quiet_arg)
endmacro()

View File

@@ -6,6 +6,8 @@ set(CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}/packages")
find_package(Pack1 REQUIRED)
find_package(Pack4 4.3 EXACT REQUIRED)
find_package(Pack7 REQUIRED)
find_package(Pack8 REQUIRED)
add_executable(FindDependency main.cpp)
target_link_libraries(FindDependency Pack1::Lib Pack4::Lib)
target_link_libraries(FindDependency Pack1::Lib Pack4::Lib Pack8::Lib)

View File

@@ -23,6 +23,18 @@
# error Expected HAVE_PACK6
#endif
#ifndef HAVE_PACK7
# error Expected HAVE_PACK7
#endif
#ifndef HAVE_PACK7_COMP1
# error Expected HAVE_PACK7_COMP1
#endif
#ifndef HAVE_PACK8
# error Expected HAVE_PACK8
#endif
int main(int argc, char** argv)
{
return 0;

View File

@@ -0,0 +1,14 @@
if(NOT Pack7_FOUND)
set(Pack7_FOUND 1)
add_library(Pack7::Pack7 INTERFACE IMPORTED)
set_property(TARGET Pack7::Pack7 PROPERTY INTERFACE_COMPILE_DEFINITIONS HAVE_PACK7)
endif()
foreach(module ${Pack7_FIND_COMPONENTS})
if(module STREQUAL "Comp1")
add_library(Pack7::Comp1 INTERFACE IMPORTED)
set_property(TARGET Pack7::Comp1 PROPERTY INTERFACE_COMPILE_DEFINITIONS HAVE_PACK7_COMP1)
set_property(TARGET Pack7::Comp1 PROPERTY INTERFACE_LINK_LIBRARIES Pack7::Pack7)
set(Pack7_Comp1_FOUND 1)
endif()
endforeach()

View File

@@ -0,0 +1,7 @@
include(CMakeFindDependencyMacro)
find_dependency(Pack7 REQUIRED COMPONENTS Comp1)
add_library(Pack8::Lib INTERFACE IMPORTED)
set_property(TARGET Pack8::Lib PROPERTY INTERFACE_COMPILE_DEFINITIONS HAVE_PACK8)
set_property(TARGET Pack8::Lib PROPERTY INTERFACE_LINK_LIBRARIES Pack7::Comp1)