target_link_libraries: Restore transitive out-of-dir linking
Refactoring in !6166 (merged) accidentally dropped the persistent lookup scope tracking across multiple items that was added by !4226 (merged). This broke a transitive out-of-dir linking case not covered by our test suite. Restore the scope tracking and add a test case.
Fixes: #22363 (closed)
Backport: release