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

Community
  • 1
  • 1
Johan
  • 20,067
  • 28
  • 92
  • 110

1 Answers1

12

Try this:

add_custom_target(flash
    COMMAND ${PERL_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/do_flash.pl ${MAIN_BIN_FILE}
    DEPENDS ${MAIN_BIN_FILE}
)
arrowd
  • 33,231
  • 8
  • 79
  • 110
  • 1
    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? – Johan Nov 09 '11 at 12:17
  • 1
    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. – arrowd Nov 09 '11 at 12:55
  • 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. – Johan Nov 09 '11 at 12:58
  • 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. – Johan Nov 09 '11 at 13:39
  • 1
    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. – arrowd Nov 09 '11 at 14:06