9

When I use the classic gnu Make I put in post build actions like flash the device (if it is a embedded device) and other similiar actions. The actual flashing is usually hidden behind a little sctipt or some commands.

Then I can type something like

make flash

so I first build the code and then it ends up on the target. The classic Makefile could have something like in it:

.PHONY: flash
flash: main.bin
    scripts/do_flash.pl main.bin

But how do I add this kind of post build actions to a cmake build?

How do I add a "custom command" that just executes a shellscript?

This questions talks about add_custom_command: The question cmake add custom command feels like it is close, but the add_custom_command seems to need a "output file" to work. But in this case there is something happening, not generated.

What would I put in the CMakeLists.txt to add such a custom action?

/Thanks


For reference, a link into the cmake documentation on this topic

1 Answer 1

12

Try this:

add_custom_target(flash
    COMMAND ${PERL_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/do_flash.pl ${MAIN_BIN_FILE}
    DEPENDS ${MAIN_BIN_FILE}
)
Sign up to request clarification or add additional context in comments.

5 Comments

btw, how do you know that ${MAIN_BIN_FILE} should transform into main.bin? Is there some kind of cmake magic, or shall I define a varialbe called MAIN_BIN_FILE and give him the value main.bin?
Yup, you should define it yourself. I assumed that you will be producing main.bin by add_custom_command(OUTPUT ${MAIN_BIN_FILE} ...). If it's not the case you can replace it with constant string. In that case you can also remove DEPENDS clause from add_custom_target.
I have not converted my project into cmake yet, I'm trying to find out if I can convert and what I will gain/lose if I do switch.
I think this can work, tricky with the internal strings that sometimes needs to have "" and sometimes not... but with some printing with the "message" command I think I can handle that.
There is difference with writing set(var string1 string2) and set(var "string1 string2"). In first case you a creating a list (';'-delimited string) with two elements and in the second case it's just plain string. Such CMake functions as add_custom_*() are accepting lists, not strings, so be сareful with quotes.

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.