conan icon indicating copy to clipboard operation
conan copied to clipboard

How can I make the CMakeDeps generator generate a CMake target for an executable?

Open betheev opened this issue 3 years ago • 2 comments
trafficstars

I have a CMake project and in the CMakeLists.txt file I generate two targets: add_library(myproj::mylib ...) and add_executable(myproj::myexec ...). The Conan documentation explains well what to write in the package_info() method in the conanfile.py file to have Conan generate a CMake target for myproj::mylib but how can I have Conan generate the CMake target myproj::myexec? I want to create a Conan package for myproj and consume it in another project. In the other project, I want to reference the executable via its target name myproj::myexec. For example:

# CMakeLists.txt
find_package(myproj REQUIRED)
add_custom_command(OUTPUT ${SOME_OUTPUT} COMMAND myproj::myexec ARGS ...)

betheev avatar Aug 01 '22 16:08 betheev

Hi @betheev,

Conan has not an explicit model to add that executable target. Probably, the best option is to create a custom build module with your target definition and then add it to the cpp_info in the package_info() method using the cmake_build_modules property, like:

def package_info(self):
    ...
    self.cpp_info.set_property("cmake_build_modules", ["<path_to_build_module.cmake>"])
    ...

Hope this helps.

czoido avatar Aug 02 '22 06:08 czoido

Hi @czoido,

thanks for your prompt response! While looking for a solution to my problem, I came across these related issues:

  • https://github.com/conan-io/conan/issues/297
  • https://github.com/conan-io/conan/issues/7240
  • https://github.com/conan-io/conan/issues/9033

What I ended up doing, in case someone else has a similar problem, was to create the target on the consumer side only if it is missing:

find_package(myproj REQUIRED)
if(NOT TARGET myproj::myexec)
    add_executable(myproj::myexec IMPORTED GLOBAL)
    set_target_properties(myproj::myexec PROPERTIES IMPORTED_LOCATION "${myproj_INCLUDE_DIR}/../bin/myexec")
endif()

The workaround assumes that the executable is contained in the Conan package.

The concrete library I was trying to install and use with Conan is flatbuffers, which provides the executable flatc.

betheev avatar Aug 02 '22 15:08 betheev