cmakeconanconanfile

conanfile.py requirements() vs build_requirements() in CMake integration


I noticed that previously I was using some conan dependencies as runtime requirements, but I only need them as build requirements, so I started using build_requirements function instead

The upgrade

# Runtime level requirements
def requirements(self):
    print("[conanfily.py]: Defining runtime requirements")
    #self.requires("grpc/1.54.3") <---OLD

# Build level requirements
def build_requirements(self):
    print("[conanfily.py]: Defining build requirements")
    self.tool_requires("grpc/1.54.3")
    self.tool_requires("protobuf/3.21.12")
    self.tool_requires("cmake/3.28.1")

Previously our build was working by simply calling find_package function in our CMake files

# Conan packages are found from conan_toolkit.cmake
find_package(gRPC CONFIG REQUIRED)
find_package(Protobuf CONFIG REQUIRED)

# Link the conan imported dependencies
target_link_libraries(${PROJECT_NAME} PRIVATE
    gRPC::grpc++ gRPC::grpc++_reflection
    protobuf::libprotobuf
)

But it no longer builds if I utilize build_requirements

The Problem:

Now that I switched to use self.tool_requires, conan 2 is no longer generating the .cmake files that I need for the find_package function

Severity    Code    Description Project File    Line    Source  Suppression State
Error       CMake Error at CmdServiceClient/CMakeLists.txt:217 (find_package):
  Could not find a package configuration file provided by "gRPC" with any of
  the following names:

    gRPCConfig.cmake
    grpc-config.cmake

  Add the installation prefix of "gRPC" to CMAKE_PREFIX_PATH or set
  "gRPC_DIR" to a directory containing one of the above files.  If "gRPC"
  provides a separate development package or SDK, be sure it has been
  installed.        C:\dev\git\LT-CONTROLLER-SERVICE\app\Service\CmdServiceClient/CMakeLists.txt    217 Build   

The .cmake files no longer exist, they don't get generated by CMakeDeps for build_requirements

Main Question:

Anyone know how I can make the generators = "CMakeDeps", "CMakeToolchain" generate .cmake files for build_requirements instead of the previous runtime conan requirements ?


Solution

  • The Conan documentation explicitly says that tool_requires are exclusively for tools, executables, like cmake or meson, and that it shouldn't be used for regular libraries requires. You can read more about this in this section in the docs

    The "Good practices" section also mention this:

    Please, do not abuse ‘tool_requires’. Those are intended only for executables like cmake and ninja running in the “build” context, not for libraries or library-like dependencies, that must use requires or test_requires.

    What you are looking for is regular requires. Conan 2 can perfectly use libraries only as "build-time", for example if an application links a static library, and the dependency will be marked as "skip" at install time, if it is not needed and downloading the binary for the static library will actually be avoided.You can read more about requirement traits, package-type