How to call a CMake function from add_custom_target/command?

时光怂恿深爱的人放手 提交于 2019-12-17 10:59:17

问题


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

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.

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.


回答1:


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
)

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




回答2:


I looked for the same and after a few minutes i realized that this is impossible because cmake is a build-generator generator.

This command will just be run at a different time, for example from within your IDE, when the cmake is just not existing.



来源:https://stackoverflow.com/questions/26024235/how-to-call-a-cmake-function-from-add-custom-target-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!