Repository: conan-io/conan

Issue:
cmake_find_package generator not forwarding all dependency properties
 Some info first:
 - conan v1.10.0
 - cmake v3.13.1
 - Linux GCC 8.1 x86_64 libstdc++11

If using CMake version >= 3.0 the `cmake_find_package` generator
generates CMake imported targets to easily import and track dependencies
of the required packages.

When using those imported targets in CMake, users get not only automatic
tracking of link dependencies **but also include directories and compile
options**. If we have three packages A, B, C, with C depending on B and
B depending on A (A <-- B <-- C) **C may require A include directories if A headers are
included in the public API of B**. In this scenario, if we are building
C with a cmake_find_package generator:

``` cmake
find_package(B REQUIRED)
add_library(C)
target_link_libraries(C PRIVATE B::B)
```

cmake correctly adds A's includes as part of C private include dirs
since the generated FindB.cmake generates not only B::B target but also
a target for A with all its properties, target that is linked against
B::B.
But if for some reason the A target (CONAN_PKG::A_A) had been defined previously,
the generator skips the generation of this target **and no longer links
A against B::B**, preventing A include dirs to be propagated to B and C.

I've found this issue when mixing bincrafters Qt package (which doesn't support the `cmake_find_package` generator) with a couple of more packages that I'm importing using the find package generator. I have to work this way since our guidelines encourage the usage of `find_package()` to require dependencies:

``` cmake
conan_basic_setup(TARGETS)
find_package(QtCore CONFIG REQUIRED)
find_package(foofromconan REQUIRED)
```
I know this use case is not common and shouldn't be encouraged, it's just an artifact of how the Qt package works right now (Our goal is to go full find_package() for transparent conan integration). But I think the "bug" in the generator could happen in different scenarios and the fix may be useful for others.

I've fixed this behavior by always adding the dependency targets to the list of targets to link (See #4125).

- [x] I've read the [CONTRIBUTING guide](https://raw.githubusercontent.com/conan-io/conan/develop/.github/CONTRIBUTING.md).
- [x] I've specified the Conan version, operating system version and any tool that can be relevant.
- [x] I've explained the steps to reproduce the error or the motivation/use case of the question/suggestion.




Discussion:

Resolving patch:
diff --git a/conans/client/generators/cmake_find_package.py b/conans/client/generators/cmake_find_package.py
--- a/conans/client/generators/cmake_find_package.py
+++ b/conans/client/generators/cmake_find_package.py
@@ -33,10 +33,10 @@
                 # Create a micro-target for each lib/a found
                 add_library(${{_LIB_NAME}} UNKNOWN IMPORTED)
                 set_target_properties(${{_LIB_NAME}} PROPERTIES IMPORTED_LOCATION ${{CONAN_FOUND_LIBRARY}})
-                list(APPEND {name}_LIBRARIES_TARGETS ${{_LIB_NAME}})
             else()
                 message(STATUS "Skipping already existing target: ${{_LIB_NAME}}")
             endif()
+            list(APPEND {name}_LIBRARIES_TARGETS ${{_LIB_NAME}})
         endif()
         message(STATUS "Found: ${{CONAN_FOUND_LIBRARY}}")
     else()


Test patch:
diff --git a/conans/test/generators/cmake_find_package_test.py b/conans/test/generators/cmake_find_package_test.py
--- a/conans/test/generators/cmake_find_package_test.py
+++ b/conans/test/generators/cmake_find_package_test.py
@@ -76,16 +76,26 @@ def cmake_lock_target_redefinition_test(self):
 set(CMAKE_CXX_COMPILER_WORKS 1)
 set(CMAKE_CXX_ABI_COMPILED 1)
 project(MyHello CXX)
-cmake_minimum_required(VERSION 2.8)
-# Create a target first to check if it is not redefined
-add_library(CONAN_LIB::Hello0_helloHello0)
+cmake_minimum_required(VERSION 3.1)
+
+# Add fake library
+add_library(fake)
+# Create an alias target to check if it is not redefined.
+# Only IMPORTED and ALIAS libraries may use :: as part of the
+# target name (See CMake policy CMP0037). This ALIAS target
+# fakes the IMPORTED targets used in the generated FindXXXX.cmake files
+add_library(CONAN_LIB::Hello0_helloHello0 ALIAS fake)
 
 find_package(Hello0 REQUIRED)
 
+get_target_property(tmp Hello0::Hello0 INTERFACE_LINK_LIBRARIES)
+message("Target libs: ${tmp}")
+
 """
         client.save(files, clean_first=True)
         client.run("create . user/channel -s build_type=Release", assert_error=True)
         self.assertIn("Skipping already existing target: CONAN_LIB::Hello0_helloHello0", client.out)
+        self.assertIn("Target libs: CONAN_LIB::Hello0_helloHello0", client.out)
 
     def cmake_find_package_test(self):
         """First package without custom find_package"""