Command wrapper¶
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