cmakeconan

How to use Conan-managed tools from `tool_requires` in CMake?


C++ project on Linux using Conan (v2) for dependencies. When using the Conan package libavrocpp/1.11.3 in conanfile.py:

def requirements(self):
    self.requires("libavrocpp/1.11.3")

def build_requirements(self):
    self.tool_requires("libavrocpp/1.11.3")

Then later I want to use the avrogencpp tool from the package in order to generate C++ classes from my avro schemas during CMake.

I can see that the tool exists (in multiple places) within the local Conan cache (~/.conan2) and I need to reference it some how from there. I could just hard-code the path to avrogencpp within my CMake files, but that would break the next time I upgrade the package to a newer version, or the Conan authors modify the layout of the cache (the cache layout is proprietary and subject to change). So:

What is the correct (version-specific) way to reference the avrogencpp tool from a CMake file when using the Conan package? Is there a symbol defined somewhere which points to it?

(CMake has FindProtobuf which defines symbols, including one which points to the version-specific protoc compiler. Is there something similar for Avro? I haven't found it so far...)


Solution

  • The conan-release and conan-debug presets automatically fix up PATH for you. In my case:

    % cmake --preset conan-release -B build/build/Release .
    ...
    Preset environment variables:
    
      DYLD_LIBRARY_PATH="/Users/botje/.conan2/p/b/libavbd87bce06f2d1/p/lib:"
      LD_LIBRARY_PATH="/Users/botje/.conan2/p/b/libavbd87bce06f2d1/p/lib:"
      PATH="/Users/botje/.conan2/p/b/libavbd87bce06f2d1/p/bin:...rest of normal path..."
    

    where .conan2/p/b/libavbd87bce06f2d1/p/bin contains the avrogencpp binary.

    You can therefore just use add_custom_command or add_custom_target and run avrogencpp without an absolute path.

    If you really want an absolute path, you can use something like:

    find_file(BIN_AVROGENCPP avrogencpp PATHS ENV PATH REQUIRED NO_DEFAULT_PATH)