钩子(Hooks)

Conan 钩子是一项旨在扩展 Conan 功能的特性,用于在包创建过程的不同阶段(如预构建和后构建)执行某些正交操作,例如质量检查。

钩子结构

钩子是一个 Python 函数,将在 Conan 工作流程的特定点执行,以自定义客户端行为,而无需修改客户端源或配方源。

这是一个简单钩子的示例

hook_example.py
 from conan.tools.files import load

 def pre_export(conanfile):
     for field in ["url", "license", "description"]:
         field_value = getattr(conanfile, field, None)
         if not field_value:
             conanfile.output.error(f"[REQUIRED ATTRIBUTES] Conanfile doesn't have '{field}'.
                                       It is recommended to add it as attribute.")

这个钩子会在配方导出之前检查其内容。基本上,pre_export() 函数会检查 conanfile 对象的属性,以查看是否存在 URL、许可证和描述。如果缺失,则通过 conanfile.output 向用户发出警告信息。这在配方导出到本地缓存之前完成。

任何 Python 脚本都可以执行。您可以创建全局函数并从不同的钩子函数中调用它们,从相对模块导入,并发出警告、错误,甚至引发异常以中止 Conan 客户端的执行。

从模块导入

钩子接口应始终放置在 Python 文件中,文件名为 hook_ 开头,扩展名为 .py。它还应该存储在 <conan_home>/extensions/hooks 文件夹中。但是,如果您的系统已安装或通过 Conan 安装了导入的模块,则可以使用其中的功能

hook_example.py
 import requests
 from conan.tools.files import replace_in_file

 def post_package(conanfile):
     if not os.path.isdir(os.path.join(conanfile.package_folder, "licenses")):
         response = requests.get('https://api.github.com/repos/company/repository/contents/LICENSE')

您还可以从相对模块导入功能

hooks
├── custom_module
│   ├── custom.py
│   └── __init__.py
└── hook_printer.py

我的 custom_module 中的 custom.py 里有

custom.py
 def my_printer(conanfile):
     conanfile.output.info("my_printer(): CUSTOM MODULE")

它可以通过导入模块在钩子中使用,就像普通的 Python 一样

hook_printer.py
 from custom_module.custom import my_printer

 def pre_export(conanfile):
     my_printer(conanfile)

钩子接口

您可以在此处查看所有可用钩子功能的完整示例

hook_full.py
 def pre_export(conanfile):
     conanfile.output.info("Running before to execute export() method.")

 def post_export(conanfile):
     conanfile.output.info("Running after of executing export() method.")

 def pre_validate(conanfile):
     conanfile.output.info("Running before executing the validate() method.")

 def post_validate(conanfile):
     conanfile.output.info("Running after executing the validate() method.")

 def pre_source(conanfile):
     conanfile.output.info("Running before to execute source() method.")

 def post_source(conanfile):
     conanfile.output.info("Running after of executing source() method.")

 def pre_generate(conanfile):
     conanfile.output.info("Running before to execute generate() method.")

 def post_generate(conanfile):
     conanfile.output.info("Running after of executing generate() method.")

 def pre_build(conanfile):
     conanfile.output.info("Running before to execute build() method.")

 def post_build(conanfile):
     conanfile.output.info("Running after of executing build() method.")

 def post_build_fail(conanfile):
     conanfile.output.info("Running after failed execution of build() method.")

 def pre_package(conanfile):
     conanfile.output.info("Running before to execute package() method.")

 def post_package(conanfile):
     conanfile.output.info("Running after of executing package() method.")

 def pre_package_info(conanfile):
     conanfile.output.info("Running before to execute package_info() method.")

 def post_package_info(conanfile):
     conanfile.output.info("Running after of executing package_info() method.")

钩子的功能旨在对它们的执行进行自描述。例如,pre_package() 函数在配方的 package() 方法执行之前调用。

所有钩子方法都只填充同一个单一对象

  • conanfile:它是一个常规的 ConanFile 对象,从接收 Conan 命令的配方中加载。它具有其正常的属性和动态对象,例如 build_folderpackage_folderoutputdependenciesoptions 等。

存储、激活和共享

钩子是存储在 <conan_home>/extensions/hooks 文件夹下的 Python 文件,其文件名应以 hook_ 开头并以 .py 扩展名结尾

一旦钩子文件存储在钩子文件夹中,钩子的激活就会自动完成。如果存储在子文件夹中,它也会自动工作。

要禁用钩子,应将其文件从钩子文件夹中删除。没有可以禁用但保留文件存储在钩子文件夹中的配置。

官方钩子

Conan 钩子 GitHub 存储库中有一些官方维护的钩子,但大多数只与 Conan 1.x 兼容,因此请先查看README以获取哪些钩子与 Conan v2 兼容的信息。