'CMake cross compiling execute custom script to flash binary into target machine

I'm running cmake on VSCode. I'm trying to set it up to invoke a custom script I wrote to flash the executable into my target mcu.

I'm reading about CROSSCOMPILING_EMULATOR, CMAKE_CROSSCOMPILING_EMULATOR and CMAKE_CROSSCOMPILING but I can't find an example of how to use them properly.

The main issue is that after the building is complete cmake is trying to run the *.elf file.

How can I flash the binary after building?



Solution 1:[1]

I have a script that does that and I execute it manually after each build. I want to invoke that script from cmake after building

Generally:

add_executable(the_executable_target ...)
add_custom_target(flashit
    COMMAND
          ${YOUR_INTERPRETER_LIKE_BASH_OR_SOMTHING}
          ./your_script 
          $<TARGET_FILE:the_executable_target>
    COMMENT "flashes it"
)
add_dependencies(flashit the_executable_taregt)

You could add ALL. You could also interest in add_custom_commnad(POST_BUILD ....

CMAKE_CROSSCOMPILING_EMULATOR is used for try_run, but most importantly for running tests.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KamilCuk