Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a CMake function from add_custom_target/command?

Tags:

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.

like image 282
Martin Avatar asked Sep 24 '14 18:09

Martin


People also ask

What does Add_custom_target do in CMake?

Adds a target with the given name that executes the given commands. The target has no output file and is always considered out of date even if the commands try to create a file with the name of the target.

How do I run a command in CMake?

From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.

What does CMake -- build do?

CMake is a cross-platform build system generator. Projects specify their build process with platform-independent CMake listfiles included in each directory of a source tree with the name CMakeLists. txt. Users build a project by using CMake to generate a build system for a native tool on their platform.


1 Answers

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.

like image 107
Adam Kosiorek Avatar answered Sep 21 '22 06:09

Adam Kosiorek