Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run custom shell script with CMake

Tags:

cmake

I am having the following directory structure:

/CMakeLists.txt
/component-a/CMakeLists.txt
           /...
/component-b/CMakeLists.txt
           /...
/doc/CMakeLists.txt
    /create-doc.sh

The shell script create-doc.sh creates a documentation file (doc.pdf). How can I use CMake to execute this shell script at build time and copy the file doc.pdf to the build directory?

I tried it by using add_custom_command in the CMakeLists.txt file inside the directory doc:

add_custom_command ( OUTPUT doc.pdf 
                     COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/create-doc.sh 
                     WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/)

Unfortunately the command is never run.

I also tried execute_process:

execute_process ( COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/create-doc.sh  
                  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ ) 

Now the script is executed during the configuration phase, but not at build time.

like image 395
Marcello90 Avatar asked Oct 10 '16 14:10

Marcello90


1 Answers

You got almost there with add_custom_command. This is indeed the correct way to tell CMake how to generate a file. However, CMake will only run that when something depends on that file.

When, as in your case, the file itself is the end product and is not used any further by subsequent build steps, the usual approach is to create a custom target to drive the relevant custom command(s):

add_custom_target(
  BuildDocs ALL
  DEPENDS doc.pdf
)

This (custom target driver for custom commands) is a very common idiom in CMake.

You can of course play around with arguments for add_custom_target (e.g. ALL, COMMENT) as it suits you.

like image 95
Angew is no longer proud of SO Avatar answered Oct 22 '22 05:10

Angew is no longer proud of SO