透明地在 tool_requires
中使用 cmake 模块¶
当我们需要重用另一个 Conan 包中的一些 .cmake
脚本时,有几种不同的可能场景,例如 .cmake
脚本是在常规的 requires
中还是在 tool_requires
中。
此外,还有两种不同的方法可供选择
脚本的消费者可以在其 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)
就会起作用。