Search code examples
c++cmakeconan

Create Conan 2 package from CMake-packaged library


I'm trying to package my CMake-packaged header-only library for Conan v2. When I say 'CMake-packaged' I mean that it comes with <project>.cmake, <project>-config.cmake, and <project>-config-version.cmake files defining exported targets and dependencies.

It has a dependency on Boost headers that are honoured when used outside of Conan (via a consuming project) but not when built with the generated Conan toolchain file.

Boost is listed as a requirement in the conanfile.py:

class arg_routerRecipe(ConanFile):
    name = "arg_router"
    ...
    default_options = {"boost/*:header_only": True}
    generators = "CMakeDeps", "CMakeToolchain"

    def requirements(self):
        self.requires("boost/[>=1.74.0]")

    ...

    def package_info(self):
        self.cpp_info.builddirs.append(os.path.join("share", "arg_router"))
        self.cpp_info.set_property("cmake_find_mode", "none")

    def package_id(self):
        self.info.settings.clear()

The Conan package is created successfully, and the deps found and installed for the test (consuming) project:

======== Computing dependency graph ========
Graph root
    conanfile.txt: /home/cmannett85/workspace/arg_router/scripts/ci/conan_test_project/conanfile.txt
Requirements
    arg_router/1.2.2#c1cb97c336cbd1652b688c26b6a310be - Cache
    boost/1.81.0#8dcd9d9df9e0d320714b83cceddbe120 - Cache
Resolved version ranges
    boost/[>=1.74.0]: boost/1.81.0

======== Computing necessary packages ========
Requirements
    arg_router/1.2.2#c1cb97c336cbd1652b688c26b6a310be:2a107742dbb22e6cb7887900af07b740e3076873#a3bd567d1bc4d9ad42ca028c14edfbf7 - Cache
    boost/1.81.0#8dcd9d9df9e0d320714b83cceddbe120:da39a3ee5e6b4b0d3255bfef95601890afd80709#b908466c911f51d126e3acf209579fc4 - Skip

======== Installing packages ========
arg_router/1.2.2: Already installed!

However, attempting to build the test project fails when the CMake package config attempts to call find_package() on Boost:

$ cmake ../arg_router/scripts/ci/conan_test_project/ -G "Ninja" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
-- Using Conan toolchain: /home/cmannett85/workspace/conan_build/conan_toolchain.cmake
-- Conan toolchain: C++ Standard 17 with extensions OFF
-- The CXX compiler identification is GNU 11.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Boost (missing: Boost_INCLUDE_DIR) (Required is at least
  version "1.74")
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindBoost.cmake:2360 (find_package_handle_standard_args)
  /usr/share/cmake-3.22/Modules/CMakeFindDependencyMacro.cmake:47 (find_package)
  /home/cmannett85/.conan2/p/arg_r7889c12afa496/p/share/arg_router/arg_router-config.cmake:32 (find_dependency)
  CMakeLists.txt:11 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/cmannett85/workspace/conan_build/CMakeFiles/CMakeOutput.log".

Looking inside conan_toolchain.cmake I can see that the arg_router dependency listed in the test project's conanfile.txt has its config file path added:

list(PREPEND CMAKE_MODULE_PATH "/home/cmannett85/.conan2/p/arg_r7889c12afa496/p/share/arg_router")

But there's no entry for Conan's Boost installation path despite it explicitly listed as a dependency in arg_router's conanfile.py. Why?


Solution

  • I asked on Conan's GitHub project page, and it was solved by:

    • Upgrading to Conan v2.0.2 (from v2.0.1)
    • Adding package_type = "header-library"