透明地在 tool_requires
内部使用 cmake 模块¶
当我们想要重用另一个 Conan 包内部的一些 .cmake
脚本时,有几种可能的不同场景,例如 .cmake
脚本是在常规的 requires
还是 tool_requires
内部。
此外,可能需要 2 种不同的方法
脚本的消费者可以在他们的 CMakeLists.txt 中执行显式的
include(MyScript)
。这种方法非常明确且设置更简单,只需在 recipe 中定义self.cpp_info.builddirs
,使用CMakeToolchain
的消费者将能够自动执行include()
并使用该功能。请参阅此处的示例消费者希望在执行
find_package()
时自动加载依赖项 cmake 模块。当前的示例实现了这种情况。
假设我们有一个包,旨在用作 tool_require
,具有以下 recipe
import os
from conan import ConanFile
from conan.tools.files import copy
class Conan(ConanFile):
name = "myfunctions"
version = "1.0"
exports_sources = ["*.cmake"]
def package(self):
copy(self, "*.cmake", self.source_folder, self.package_folder)
def package_info(self):
self.cpp_info.set_property("cmake_build_modules", ["myfunction.cmake"])
以及 myfunction.cmake
文件,位于
function(myfunction)
message("Hello myfunction!!!!")
endfunction()
我们可以执行 cd myfunctions && conan create .
,这将创建包含 cmake 脚本的 myfunctions/1.0
包。
然后,消费者包将如下所示
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain
class Conan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
tool_requires = "myfunctions/1.0"
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
deps = CMakeDeps(self)
# By default 'myfunctions-config.cmake' is not created for tool_requires
# we need to explicitly activate it
deps.build_context_activated = ["myfunctions"]
# and we need to tell to automatically load 'myfunctions' modules
deps.build_context_build_modules = ["myfunctions"]
deps.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
以及像这样的 CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(test)
find_package(myfunctions CONFIG REQUIRED)
myfunction()
然后,消费者将能够自动调用依赖模块中的 myfunction()
$ conan build .
...
Hello myfunction!!!!
如果由于某种原因,消费者想要强制从 tool_requires()
作为 CMake 模块使用,消费者可以执行 deps.set_property("myfunctions", "cmake_find_mode", "module", build_context=True)
,然后 find_package(myfunctions MODULE REQUIRED)
将会起作用。