conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
8.29k stars 982 forks source link

[question] PkgConfig.fill_cpp_info() Overwrite Existing self.cpp_info #17362

Open tianyi-lu-ocean opened 21 hours ago

tianyi-lu-ocean commented 21 hours ago

What is your question?

I have a producer package which uses a system library libcamera. To achieve that, I added the following lines in its CMakeLists.txt,

find_package(PkgConfig)
# Then try to find system libcamera to find the corresponding cameras
pkg_check_modules(LIBCAMERA REQUIRED IMPORTED_TARGET libcamera)
message(STATUS "libcamera library found:")
message(STATUS "    version: ${LIBCAMERA_VERSION}")
message(STATUS "    libraries: ${LIBCAMERA_LINK_LIBRARIES}")
message(STATUS "    include path: ${LIBCAMERA_INCLUDE_DIRS}")

however, in the consumer package's CMakeLists.txt, I have to do the same thing which is quite annoying. I know I can leverage find_dependency() in CMake, but as I rely on CMakeDeps generator, I don't know how to add that into the auto generated <PackageName>Config.cmake file.

After some investigation, I found a recipe tool PkgConfig in conan.tools.gnu category. So, I think I can do something in producer's recipe, like,

def package_info(self);
    pkg_config = PkgConfig(self, 'libcamera')
    pkg_config.fill_cpp_info(self.cpp_info, is_system = True)

and it probably will propagate the include path and library path into consumer. in which case I don't have to duplicate those CMake stuffs in consumer's CMakeLists.txt.

However, it doesn't work, because the PkgConfig.fill_cpp_info() will overwrite the self.cpp_info. I also looked into the source code of PkgConfig, it applies assignment operation for those lists in cpp_info, please see the line here, I guess line 116 could be a potential bug, because it tries to assign a different list to the cpp_info.cxxflags, or maybe the pkg-config does not support cxxflags? Another issue would be the comment of system_libs parameter in fill_cpp_info() which also affects the conan documentation, it seems like just copying the comments of 'is_system`.

Anyway, my questions are,

Incidentally, I tried to pass a temporary cpp_info into it, but due to Python passing by reference feature, it will still overwrite the original self.cpp_info. Besides that, I also tried to copy.deepcopy(), but for some reason, it gives me a TypeError exception. So, my current solution is,

I don't think this is a decent solution, I would expect to do those in fill_cpp_info().

Have you read the CONTRIBUTING guide?