Repository: conan-io/conan
Pull request: [fix] Removed CMake warning message
Changelog: Fix: Deleted CMake warning for already existing targets.
Closes: https://github.com/conan-io/conan/issues/10132
Docs: omit

- [x] Refer to the issue that supports this Pull Request.
- [x] If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
- [x] I've read the [Contributing guide](https://github.com/conan-io/conan/blob/develop/.github/CONTRIBUTING.md).
- [x] I've followed the PEP8 style guides for Python code.
- [ ] I've opened another PR in the Conan docs repo to the ``develop`` branch, documenting this one. 

<sup>**Note:** By default this PR will skip the slower tests and will use a limited set of python versions. Check [here](https://github.com/conan-io/conan/blob/develop/.github/PR_INCREASE_TESTING.md) how to increase the testing level by writing some tags in the current PR body text.</sup>


Problem statement:
[bug] CMakeToolchain/CMakeDeps generates CMake warnings when dependency was already found via find_package
### Environment Details (include every applicable attribute)
  * Operating System+version: Linux Mint 20.1 Ulyssa
  * Compiler+version: gcc 11.1.0
  * Conan version: 1.43.0
  * Python version: 3.8.10

### Steps to reproduce (Include if Applicable)
Use the CMakeToolchain and CMakeDeps generator.
Use the generated conan_toolchain.cmake file then `find_package` for a dependency and then `find_package` for a transitive dependency. E.g.
```cmake
find_package(PNG 1.6 REQUIRED)
find_package(Freetype 2.10 REQUIRED)
```

In this example, Freetype depends on PNG.

This could possibly be avoided by omitting the `find_package` for `PNG` (in this case), but that's not a good solution in case the consumer depends on `PNG` (or other transitively dependent library) and can't depend on the library being included transitively.

Also, adding `QUIET` to the `find_package` calls does not suppress the warning because they are emitted directly via `message(WARNING)` and so cannot be suppressed with that flag.

### Logs (Executed commands with output) (Include/Attach if Applicable)

```
CMake Warning at cmake-build-debug/zlibTargets.cmake:24 (message):
  Target name 'zlib::zlib' already exists.
Call Stack (most recent call first):
  cmake-build-debug/zlib-config.cmake:11 (include)
  [omitted path to cmake]/cmake-3.21/Modules/CMakeFindDependencyMacro.cmake:47 (find_package)
  cmake-build-debug/libpng-config.cmake:16 (find_dependency)
  [omitted path to cmake]/cmake-3.21/Modules/CMakeFindDependencyMacro.cmake:47 (find_package)
  cmake-build-debug/freetype-config.cmake:16 (find_dependency)
  CMakeLists.txt:83 (find_package)
  ```

Hints from issue discussion:
I think the issue is doing a ``find_package()`` for a transitive dependency not directly declared in your ``conanfile.py``.
Conan 2.0 is going to completely isolate from transitive dependencies, that ``find_package()`` will probably fail because the headers will not be visible. This feature has been requested many times in the past, because it is necessary to maintain good architecture and good practices among developers. If you need to directly use PNG in your code, you need to add a ``requires="png..."`` in your recipe. 

Please try that, adding PNG as ``requires``, and the warnings should probably go away, please let us know.
Hmm..., my recipe already has png -- sorry I didn't post it earlier:

```python
requires = [
        "fmt/8.0.1",
        "vulkan-loader/1.2.198.0",
        "opengl/system",
        "glslang/8.13.3559",
        "freetype/2.10.4",
        "pugixml/1.11",
        "spirv-cross/20210115",
        "zlib/1.2.11",
        "libpng/1.6.37",
# additional dependencies...
    ]
```

I tried re-ordering them, just in case it would matter but there was no change.

Looking at the generated cmake, it seems pretty apparent why the warning is throwing:

```cmake
if(NOT TARGET zlib::zlib)
    add_library(zlib::zlib INTERFACE IMPORTED)
    conan_message(STATUS "Conan: Target declared 'zlib::zlib'")
else()
    message(WARNING "Target name 'zlib::zlib' already exists.")
endif()
```

Since `zlib::zlib` was already generated by a previous `find_package` from `PNG` the second time it's called it will execute the else. I think the correct behavior would be to just not emit a warning. I believe normal behavior for cmake `find_package` is to just silently re-use the previously found package, so omitting the warning would bring the behavior closer to standard cmake.
@franramirez688 you were checking last time about this warning with @lasote, do you see any problem in omitting the warning?
Yeah, let's remove it. We didn't realize about this use case.

Commit message: Keeping aliases warning

Patch to review:
diff --git a/conan/tools/cmake/cmakedeps/templates/targets.py b/conan/tools/cmake/cmakedeps/templates/targets.py
index 2f3336ab2d8..007881edbcb 100644
--- a/conan/tools/cmake/cmakedeps/templates/targets.py
+++ b/conan/tools/cmake/cmakedeps/templates/targets.py
@@ -75,8 +75,6 @@ def template(self):
         if(NOT TARGET {{ root_target_name }})
             add_library({{ root_target_name }} INTERFACE IMPORTED)
             conan_message(STATUS "Conan: Target declared '{{ root_target_name }}'")
-        else()
-            message(WARNING "Target name '{{root_target_name}}' already exists.")
         endif()
 
         {%- for alias, target in cmake_target_aliases.items() %}
diff --git a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_components_names.py b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_components_names.py
index 60ce2351a56..ec57b40db39 100644
--- a/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_components_names.py
+++ b/conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_components_names.py
@@ -899,4 +899,4 @@ def build(self):
     client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists})
     client.run("create .")
 
-    assert "Target name 'mypkg::name' already exists." in client.out
+    assert "Target name 'mypkg::name' already exists." not in client.out


Code review comments:
diff_hunk: @@ -899,4 +899,4 @@ def build(self):
     client.save({"conanfile.py": consumer, "CMakeLists.txt": cmakelists})
     client.run("create .")
 
-    assert "Target name 'mypkg::name' already exists." in client.out
+    assert "Target name 'mypkg::name' already exists." not in client.out
path: conans/test/functional/toolchains/cmake/cmakedeps/test_cmakedeps_components_names.py
text: @author:
I was doubting if keeping these tests or not... I think it could be helpful to have them. Wdyt?

@user1:
Negative "not in" checks are low value, because of false positives. The message can change to "hello::hello exists" and it would still be true. Not harm to leave them though, you can leave them, no problem.

@author:
You're right. Then, let's get rid of it.