Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
betheev opened this issue Aug 1, 2022 · 3 comments
Closed
1 task done
Assignees

Comments

@betheev
Copy link

betheev commented Aug 1, 2022

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 ...)
@czoido czoido self-assigned this Aug 2, 2022
@czoido
Copy link
Contributor

czoido commented Aug 2, 2022

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.

@betheev
Copy link
Author

betheev commented Aug 2, 2022

Hi @czoido,

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

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.

@memsharded
Copy link
Member

Conan 2.9 introduced a new CMakeDeps generator that generate targets for executables: #16964

It is in dev-state at the moment, enabled with -c tools.cmake.cmakedeps:new=will_break_next (the value changes in every release to guarantee dev-status and not usable in production until we get enough feedback).

I am closing this ticket as solved, feedback very welcome, please create new tickets for any feedback or issue related to this new CMakeDeps generator, many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants