Repository: conan-io/conan

Issue:
[bug] CONAN_LIBS is overwritten when running conan_basic_setup()
<!--
  Please don't forget to update the issue title.
  Include all applicable information to help us reproduce your problem.

  To help us debug your issue please explain:
-->
We have a very specific setup where we specify platform dependencies in the cmake toolchain file included in the platform's toolchain.

Essentially, we do `set(CONAN_LIBS ${CONAN_LIBS} ourDependency)` to pre-populates the variable with our dependency. Our CMakeLists.txt proceeds normally and calls `conan_basic_setup()` (using global variables).

This used to work properly in 1.19.2, however we noticed in 1.20+ that our dependency completely disappears from `CONAN_LIBS`.

I noticed these peculiar line in our `conanbuildinfo.cmake`:
```
...
# Apple Frameworks
conan_find_apple_frameworks(CONAN_FRAMEWORKS_FOUND "${CONAN_FRAMEWORKS}")
# Append to aggregated values variable
set(CONAN_LIBS ${CONAN_PKG_LIBS} ${CONAN_SYSTEM_LIBS} ${CONAN_FRAMEWORKS_FOUND})


###  Definition of macros and functions ###

macro(conan_define_targets)
...
```

It looks like the `CONAN_LIBS` variable is getting completely overwritten. It works for any package defined in our `conanfile.txt`, however any values appended manually before the include of `conanbuildinfo.cmake` are lost.

According to the comment, it is supposed to append to the aggregated values, not to overwrite them.

When I look at [this PR](https://github.com/conan-io/conan/pull/6003/files#diff-4ace401e8414c1a6556986dc05a638d8R117), this seems to have been introduced in 1.20.

### Environment Details (include every applicable attribute)
  * Operating System+version: Ubuntu 18.04
  * Compiler+version:
  * Conan version: 1.21.1
  * Python version: 3.6.9

### Steps to reproduce (Include if Applicable)

- Create an example project using conan and cmake (using the [global variable approach](https://docs.conan.io/en/latest/integrations/build_system/cmake/cmake_generator.html#global-variables-approach))
- Add `set(CONAN_LIBS ${CONAN_LIBS} phtread)` (or any other system library) right after `cmake_minimum_required(...)`
- Add `message("CONAN_LIBS = ${CONAN_LIBS}")` after `conan_basic_setup()`.
- Run cmake configure (I don't think there should be a difference between running `cmake ..` and using the helper). You should notice that pthread (or whatever library) is not printed, meaning the variable has been completely replaced (and thus the library will be missing in `target_link_libraries`)


### Logs (Executed commands with output) (Include/Attach if Applicable)

<!--
  Your log content should be related to the bug description, it can be:
  - Conan command output
  - Server output (Artifactory, conan_server)
-->



Discussion:
Thanks for the detailed report @ostjean 

Yes, it seems this might be a regression, please @danimtb, confirm when possible. In that case we should be adding CONAN_LIBS in that line too.

Resolving patch:
diff --git a/conans/client/generators/cmake_common.py b/conans/client/generators/cmake_common.py
--- a/conans/client/generators/cmake_common.py
+++ b/conans/client/generators/cmake_common.py
@@ -99,7 +99,7 @@ def cmake_dependencies(dependencies, build_type=""):
 set(CONAN_FRAMEWORK_DIRS{build_type} {deps.framework_paths} ${{CONAN_FRAMEWORK_DIRS{build_type}}})
 set(CONAN_LIBS{build_type} {deps.libs} ${{CONAN_LIBS{build_type}}})
 set(CONAN_PKG_LIBS{build_type} {deps.libs} ${{CONAN_PKG_LIBS{build_type}}})
-set(CONAN_SYSTEM_LIBS{build_type} {deps.system_libs})
+set(CONAN_SYSTEM_LIBS{build_type} {deps.system_libs} ${{CONAN_SYSTEM_LIBS{build_type}}})
 set(CONAN_FRAMEWORKS{build_type} {deps.frameworks} ${{CONAN_FRAMEWORKS{build_type}}})
 set(CONAN_FRAMEWORKS_FOUND{build_type} "")  # Will be filled later
 set(CONAN_DEFINES{build_type} {deps.defines} ${{CONAN_DEFINES{build_type}}})
@@ -113,8 +113,8 @@ def cmake_dependencies(dependencies, build_type=""):
 
 # Apple Frameworks
 conan_find_apple_frameworks(CONAN_FRAMEWORKS_FOUND{build_type} "${{CONAN_FRAMEWORKS{build_type}}}")
-# Append to aggregated values variable
-set(CONAN_LIBS{build_type} ${{CONAN_PKG_LIBS{build_type}}} ${{CONAN_SYSTEM_LIBS{build_type}}} ${{CONAN_FRAMEWORKS_FOUND{build_type}}})
+# Append to aggregated values variable: Use CONAN_LIBS instead of CONAN_PKG_LIBS to include user appended vars
+set(CONAN_LIBS{build_type} ${{CONAN_LIBS{build_type}}} ${{CONAN_SYSTEM_LIBS{build_type}}} ${{CONAN_FRAMEWORKS_FOUND{build_type}}})
 """
 
 


Test patch:
diff --git a/conans/test/functional/generators/cmake_multi_test.py b/conans/test/functional/generators/cmake_multi_test.py
--- a/conans/test/functional/generators/cmake_multi_test.py
+++ b/conans/test/functional/generators/cmake_multi_test.py
@@ -302,7 +302,7 @@ class Consumer(ConanFile):
         client.run("create conanfile_mylib.py mylib/1.0@us/ch")
         client.run("install conanfile_consumer.py -s build_type=Release")
         content = client.load("conanbuildinfo_release.cmake")
-        self.assertIn("set(CONAN_LIBS_RELEASE ${CONAN_PKG_LIBS_RELEASE} ${CONAN_SYSTEM_LIBS_RELEASE}"
+        self.assertIn("set(CONAN_LIBS_RELEASE ${CONAN_LIBS_RELEASE} ${CONAN_SYSTEM_LIBS_RELEASE}"
                       " ${CONAN_FRAMEWORKS_FOUND_RELEASE})", content)
         self.assertIn("set(CONAN_PKG_LIBS_RELEASE lib1 lib1release ${CONAN_PKG_LIBS_RELEASE})",
                       content)
@@ -310,19 +310,19 @@ class Consumer(ConanFile):
                       "${CONAN_SYSTEM_LIBS_MYLIB_RELEASE} ${CONAN_FRAMEWORKS_FOUND_MYLIB_RELEASE})",
                       content)
         self.assertIn("set(CONAN_PKG_LIBS_MYLIB_RELEASE lib1 lib1release)", content)
-        self.assertIn("set(CONAN_SYSTEM_LIBS_RELEASE sys1)", content)
+        self.assertIn("set(CONAN_SYSTEM_LIBS_RELEASE sys1 ${CONAN_SYSTEM_LIBS_RELEASE})", content)
         self.assertIn("set(CONAN_SYSTEM_LIBS_MYLIB_RELEASE sys1)", content)
 
         client.run("install conanfile_consumer.py -s build_type=Debug")
         content = client.load("conanbuildinfo_debug.cmake")
-        self.assertIn("set(CONAN_LIBS_DEBUG ${CONAN_PKG_LIBS_DEBUG} ${CONAN_SYSTEM_LIBS_DEBUG} "
+        self.assertIn("set(CONAN_LIBS_DEBUG ${CONAN_LIBS_DEBUG} ${CONAN_SYSTEM_LIBS_DEBUG} "
                       "${CONAN_FRAMEWORKS_FOUND_DEBUG})", content)
         self.assertIn("set(CONAN_LIBS_MYLIB_DEBUG ${CONAN_PKG_LIBS_MYLIB_DEBUG} "
                       "${CONAN_SYSTEM_LIBS_MYLIB_DEBUG} ${CONAN_FRAMEWORKS_FOUND_MYLIB_DEBUG})",
                       content)
         self.assertIn("set(CONAN_PKG_LIBS_DEBUG lib1 lib1debug ${CONAN_PKG_LIBS_DEBUG})", content)
         self.assertIn("set(CONAN_PKG_LIBS_MYLIB_DEBUG lib1 lib1debug)", content)
-        self.assertIn("set(CONAN_SYSTEM_LIBS_DEBUG sys1d)", content)
+        self.assertIn("set(CONAN_SYSTEM_LIBS_DEBUG sys1d ${CONAN_SYSTEM_LIBS_DEBUG})", content)
         self.assertIn("set(CONAN_SYSTEM_LIBS_MYLIB_DEBUG sys1d)", content)
 
 
diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py
--- a/conans/test/functional/generators/cmake_test.py
+++ b/conans/test/functional/generators/cmake_test.py
@@ -141,12 +141,12 @@ class Consumer(ConanFile):
         client.run("install conanfile_consumer.py")
 
         content = client.load("conanbuildinfo.cmake")
-        self.assertIn("set(CONAN_LIBS ${CONAN_PKG_LIBS} ${CONAN_SYSTEM_LIBS} ${CONAN_FRAMEWORKS_FOUND})",
+        self.assertIn("set(CONAN_LIBS ${CONAN_LIBS} ${CONAN_SYSTEM_LIBS} ${CONAN_FRAMEWORKS_FOUND})",
                       content)
         self.assertIn("set(CONAN_LIBS_MYLIB ${CONAN_PKG_LIBS_MYLIB} ${CONAN_SYSTEM_LIBS_MYLIB} ${CONAN_FRAMEWORKS_FOUND_MYLIB})",
                       content)
         self.assertIn("set(CONAN_PKG_LIBS_MYLIB lib1)", content)
-        self.assertIn("set(CONAN_SYSTEM_LIBS sys1)", content)
+        self.assertIn("set(CONAN_SYSTEM_LIBS sys1 ${CONAN_SYSTEM_LIBS})", content)
         self.assertIn("set(CONAN_SYSTEM_LIBS_MYLIB sys1)", content)
 
         # Check target has libraries and system deps available
@@ -223,3 +223,54 @@ def build(self):
                       "$<$<CONFIG:RelWithDebInfo>:;>;$<$<CONFIG:MinSizeRel>:;>;$<$<CONFIG:Debug>:;>",
                       client.out)
         self.assertIn("CONAN_LIB::myotherlib_lib2 libs: sys2;CONAN_PKG::mylib", client.out)
+
+    def user_appended_libs_test(self):
+        conanfile = textwrap.dedent("""
+            import os
+            from conans import ConanFile, CMake
+
+            class MyLib(ConanFile):
+                name = "mylib"
+                version = "1.0"
+                settings = "os", "compiler", "arch", "build_type"
+                exports_sources = "lib1.a", "lib1.lib"
+
+                def package(self):
+                    self.copy("*", dst="lib")
+
+                def package_info(self):
+                    self.cpp_info.system_libs = ["sys1"]
+                    self.cpp_info.libs = ["lib1"]
+                """)
+        consumer = textwrap.dedent("""
+            import os
+            from conans import ConanFile, CMake
+
+            class Consumer(ConanFile):
+                name = "consumer"
+                version = "1.0"
+                generators = "cmake"
+                settings = "os", "compiler", "arch", "build_type"
+                exports_sources = "CMakeLists.txt"
+                requires = "mylib/1.0"
+
+                def build(self):
+                    cmake = CMake(self)
+                    cmake.configure()
+                """)
+        cmakelists = textwrap.dedent("""
+            cmake_minimum_required(VERSION 3.1)
+            project(consumer CXX)
+            set(CONAN_LIBS additional_lib)
+            set(CONAN_SYSTEM_LIBS additional_sys)
+            include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+            message("CONAN_LIBS: ${CONAN_LIBS}")
+            message("CONAN_SYSTEM_LIBS: ${CONAN_SYSTEM_LIBS}")
+            """)
+        client = TestClient()
+        client.save({"conanfile.py": conanfile, "lib1.lib": "", "liblib1.a": "",
+                     "consumer.py": consumer, "CMakeLists.txt": cmakelists})
+        client.run("create .")
+        client.run("create consumer.py")
+        self.assertIn("CONAN_LIBS: lib1;additional_lib;sys1;additional_sys", client.out)
+        self.assertIn("CONAN_SYSTEM_LIBS: sys1;additional_sys", client.out)
diff --git a/conans/test/unittests/client/generators/cmake_test.py b/conans/test/unittests/client/generators/cmake_test.py
--- a/conans/test/unittests/client/generators/cmake_test.py
+++ b/conans/test/unittests/client/generators/cmake_test.py
@@ -345,7 +345,7 @@ def apple_frameworks_test(self):
                       ' ${CONAN_FRAMEWORK_DIRS})', content)
         self.assertIn('set(CONAN_FRAMEWORK_DIRS "path/to/Frameworks1"\n\t\t\t"path/to/Frameworks2" '
                       '${CONAN_FRAMEWORK_DIRS})', content)
-        self.assertIn('set(CONAN_LIBS ${CONAN_PKG_LIBS} ${CONAN_SYSTEM_LIBS} '
+        self.assertIn('set(CONAN_LIBS ${CONAN_LIBS} ${CONAN_SYSTEM_LIBS} '
                       '${CONAN_FRAMEWORKS_FOUND})', content)
 
         generator = CMakeFindPackageGenerator(conanfile)