如何从 add_custom_target/command 调用 CMake 函数?

2021-12-26 00:00:00 cmake c++ target add-custom-command

是否可以从 add_custom_targetadd_custom_command 中调用 CMake 函数?

Is it possible to call a CMake function out of an add_custom_target or add_custom_command?

我知道我可以将 CMake 函数移动到 Python(或其他)脚本并从 add_custom_target/command 调用它,但我想避免大量脚本在现有的 CMake 基础设施旁边.

I know I could move the CMake function to a Python (or whatever) script and call it from add_custom_target/command but I would like to avoid having tons of script next to the existing CMake infra.

我想要实现的是使用 CPack 生成二进制工件的 zip 包并将它们发布到工件存储库中.对于发布部分,我已经创建了 CMake 函数,但现在我需要将打包和发布结合在一起.

What I want to achieve is to use CPack for generating a zip package of binary artifacts and publish them in an artifact repository. For the publishing part I have already the CMake function created but now I need to combine the packaging and publishing together.

提前感谢您的任何帮助/提示.

Thanks for any help/hints in advance.

推荐答案

我在为 BVLC/Caffe.我最终做的是将函数内容放入一个单独的 CMake 脚本中,并通过调用从 add_custom_target 中调用它:

I encountered this issue while writing a CMake build system for BVLC/Caffe. What I finally did is I put the function content into a separate CMake script and called it from within add_custom_target by invoking:

add_custom_target(target_name
    COMMAND ${CMAKE_COMMAND} -P path_to_script
)

使用 -P 标志调用 CMake 使其充当脚本语言.您可以在脚本中放置任何 CMake 函数.

Invoking CMake with -P flag makes it act as a scripting language. You can put any CMake functions inside the script.

相关文章