Issue
Using Conan v2.0.14 and CMake v3.27.8 I'd like to create a reusable "only-CMake-code" Conan package (foo
) that can be consumed by another CMake project (bar
) and allows to use the include([...])
command in the CMakeLists.txt
of the bar
project in order to pull in CMake modules defined in foo
.
I assume (I'm totally new to Conan, but have good knowledge of CMake) the way to go is to create a proper CMake package (either via Conan or via CMakeLists.txt
) for foo
that generates a foo-config.cmake
file. This should allow bar
to find foo
using the find_package(foo REQUIRED)
command. Afterwards a CMake variable defined in foo-config.cmake
file should be accessible in bar
, allowing the following in the CMakeLists.txt
of bar
:
include("${foo_ROOT}/foo_module.cmake")
My current conanfile.py
of foo
looks as follows:
from os import path
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain
from conan.tools.files import copy
required_conan_version = ">=2.0.14"
class Recipe(ConanFile):
name = "foo"
version = "latest"
exports_sources = "tool/cmake/*"
no_copy_source = True
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
def package(self):
cmake = CMake(self)
cmake.install()
copy(self, "tool/cmake/*", self.source_folder, self.package_folder)
def package_info(self):
self.cpp_info.bindirs = []
self.cpp_info.libdirs = []
Running conan create
outputs the following error:
cmake.install()
ConanException: build_type setting should be defined.
I do understand the message, but now I think maybe I've chosen the wrong approach. I'd like to simply install foo
using a CMake Package Config, but do not want to care about build configs, toolchains, etc. at all. I'm also unsure whether foo
also requires a CMakeLists.txt
(maybe with additional install
commands) in order to be properly installable via Conan.
I've read the related question How to use CMake file provided by a Conan package? and its answers but it didn't help me.
Solution
There are a couple of examples in the docs that might be useful:
- using a macro from a regular dependency
requires
, see this docs docs.conan.io/2/examples/graph/requires/… - Using a macro from a tool_requires
If the intent is to reuse some script, it might not be necessary to do a find_package()
and make the xxx-config.cmake scripts bring the modules to use, but it is probably easier to do a plain include(myscript)
, because the toolchains like CMakeToolchain
will put the necessary paths in the right variables so this works (the dependency recipe has to define self.cpp_info.builddirs
to point to the folders containing the scripts)
Answered By - drodri Answer Checked By - Clifford M. (WPSolving Volunteer)