命令包装器¶
扩展插件 cmd_wrapper.py 是一个 Python 脚本,它接收由 self.run() recipe 调用提供的命令行参数,并允许拦截它们并返回一个新的参数。
此插件必须位于 extensions/plugins 缓存文件夹中,可以使用 conan config install 命令进行安装。
例如
def cmd_wrapper(cmd, **kwargs):
return 'echo "{}"'.format(cmd)
它将拦截命令并在终端显示,这意味着所有 recipe 中的 self.run() 命令都不会执行,而是会被回显。
**kwargs 是一个强制性的通用参数,为了应对未来的变化和 Conan 的新关键字参数注入而保持健壮。即使未使用它,不添加它也可能导致扩展在未来的 Conan 版本中失败。
更常见的用例是在某些命令上注入并行化工具,这可能如下所示
def cmd_wrapper(cmd, **kwargs):
# lets parallelize only CMake invocations
if cmd.startswith("cmake"):
return 'parallel-build "{}" --parallel-argument'.format(cmd)
# otherwise return same command, not modified
return cmd
conanfile 对象作为参数传递,因此可以根据调用者自定义行为
def cmd_wrapper(cmd, conanfile, **kwargs):
# Let's parallelize only CMake invocations, for a few specific heavy packages
name = conanfile.ref.name
heavy_pkgs = ["qt", "boost", "abseil", "opencv", "ffmpeg"]
if cmd.startswith("cmake") and name in heavy_pkgs:
return 'parallel-build "{}" --parallel-argument'.format(cmd)
# otherwise return same command, not modified
return cmd