find_dependency: Small regression introduced by !3161
!3161 (merged), introduces a small regression, since the behaviour is slightly changed in a situation like this:
find_package(A COMPONENTS FOO BAR)
# The variable `A_LIBRARIES` now contains the libraries for the components searched last time, i.e. "A::FOO;A::BAR"
find_package(B)
# internally B does
# find_dependency(A COMPONENTS ZOT)
# [...]
target_link_libraries(TheTarget PRIVATE ${A_LIBRARIES})
before the change, the content of the variable was (as the user would expect) still A::FOO;A::BAR
, but now the content is just A::ZOT
, since it now contains the libraries for the component searched last time, and the target now fails to compile.
If, instead of contains the libraries for the components searched last time, the targets were just appended (i.e. A::FOO;A::BAR;A::ZOT
), then the behaviour would still be different, since user would be linking to an extra library that is not needed, and might cause other kind of issues.
Even though I agree 100% that using targets is the right way to fix this issue in this specific case, there is a lot of legacy code that uses it in this way. Also I'm afraid that this could be an issue for other variables, not just in this specific case.
I don't know if this is possible, but in order to restore the old behaviour, the value of any variable modified inside a find_dependency
should be restored when the calling find_package
returns (which is slightly different from just adding a scope, since all the other variables modified inside the find_package
should not be modified, but find_package
should still be able to use these variables).
A more realistic possibility would be to add a variable scope to find_package
, a CMake policy to enable/disable the new behaviour, and perhaps an extra option NO_VARIABLE_SCOPE
to find_package
.
Nonetheless this would require to change all existing FindPackage.cmake
and CMakeConfig.cmake
to export the variables in their parent scope, instead of just setting them, and I don't know if such a change is acceptable.