14

How to execute a shell script using CMake? The command that should be run is my_script that should be executed after build. The CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

project(abc)

include_directories("/usr/lib/avr/include")

set(CMAKE_CURRENT_SOURCE_DIR /home/user/Desktop)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmcu=atmega8")
set(SOURCE_FILES main.c)

add_executable(abc ${SOURCE_FILES})

#not working ----->
add_custom_command(TARGET abc
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E my_script
        )

DISCLAIMER

Yes, there are similar questions in SO here, here, here, etc; however, they don't give me a clear vision how this can be achieved.

1 Answer 1

24

You are invoking CMake with it's command-line tool mode which doesn't execute generic scripts or commands.

Instead do e.g.

add_custom_command(TARGET abc
        POST_BUILD
        COMMAND /bin/sh /path/to/my_script
        )
Sign up to request clarification or add additional context in comments.

3 Comments

Elegant answer, just perfect :)
Is there a way to use a relative, instead of an absolute path to the script?
@MattKelly Of course, but the relatives paths will be from the build directory. Better use one of the CMake directory variables instead (like ${PROJECT_SOURCE_DIR} or similar).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.