Executing a Post Build Batch File using CMake


Bolivar, Marti
 

Hi Casey,

Did you have a look at the global 'extra_post_build_commands' and
'extra_post_build_byproducts' properties mentioned in
zephyr/CMakeLists.txt?

Try searching around the source tree to see how it is used and if you
can adapt it to your needs.

HTH,
Martí

On Wed, Apr 13 2022, Casey Shea via lists.zephyrproject.org wrote:
*Build Environment Details*

* Building Zephyr with CMake
* ARM Platform (Nordic nrf5340)
* Source and Build directories are out of tree.

* My build directory is ${ CMAKE_CURRENT_SOURCE_DIR }../../Executables
* My source directories are in several places, but all are in a sub-directory of ${ CMAKE_CURRENT_SOURCE_DIR }/../../../../

* CLion is my IDE of choice but I don't believe that matters for this discusson.

*What I am Trying to Achieve*
At the end of my build process, I would like to execute a batch file on the zephyr_final target that signs the final image.  This is different than the MCU Boot signature and is a custom proprietary signature.  I am unable to change the way in which this image is signed as it must be compatible with existing tools used with my product.

*Method in Which I am trying to Achieve*
Normally, I would do this using add_custom_command(TARGET target_name POST_BUILD
COMMAND "batchfile.bat" batchfile_arguments)

However, I don't believe that the final zephyr target is within the scope of the top-level CMakeLists.txt file.  That said, I do not have a lot of experience with CMake as most of my past development was within the IAR toolchain.  It's very possible that I am misinterpreting this.

Questions
1.  Will I be able to use the add_custom_command() CMake function to execute the batch file?  If so, can someone provide an example as to how I would do this and what Target I should be using.

2.  Is there a better, more appropriate method for doing this within the Zephyr environment.

Any help or guidance is much appreciated!



mlsvrts
 

Hi Casey,

I have been using `add_custom_command` to sign my generated images like this:

set(ZEPHYR_SIGNED_BIN ${ZEPHYR_BINARY_DIR}/zephyr.signed.bin)
set(MCUBOOT_SIGNING_KEY ${ZEPHYR_BASE}/../bootloader/mcuboot/root-rsa-2048.pem)
set(APP_VERSION 1.0.5+2)

add_custom_command(
    COMMAND west sign -t imgtool --
        --key ${MCUBOOT_SIGNING_KEY}
        --version ${APP_VERSION}
    OUTPUT ${ZEPHYR_SIGNED_BIN}
    DEPENDS zephyr_final
    COMMENT "Running post-build signing step..."
)

add_custom_target(
    post_bin ALL
    DEPENDS
    ${ZEPHYR_SIGNED_BIN}
)


Note that the command won't trigger without the additional `custom_target`. Also, my source directory is child of `zephyrproject/zephyr`, so I can't say for certain that this will work in your case.

- mlsvrts


Casey Shea
 

Build Environment Details
  • Building Zephyr with CMake
  • ARM Platform (Nordic nrf5340)
  • Source and Build directories are out of tree.
    • My build directory is ${CMAKE_CURRENT_SOURCE_DIR}../../Executables
    • My source directories are in several places, but all are in a sub-directory of ${CMAKE_CURRENT_SOURCE_DIR}/../../../../
  • CLion is my IDE of choice but I don't believe that matters for this discusson.

What I am Trying to Achieve
At the end of my build process, I would like to execute a batch file on the zephyr_final target that signs the final image.  This is different than the MCU Boot signature and is a custom proprietary signature.  I am unable to change the way in which this image is signed as it must be compatible with existing tools used with my product.

Method in Which I am trying to Achieve
Normally, I would do this using add_custom_command(TARGET target_name POST_BUILD 
                                                                           COMMAND "batchfile.bat" batchfile_arguments)

However, I don't believe that the final zephyr target is within the scope of the top-level CMakeLists.txt file.  That said, I do not have a lot of experience with CMake as most of my past development was within the IAR toolchain.  It's very possible that I am misinterpreting this.  

Questions
1.  Will I be able to use the add_custom_command() CMake function to execute the batch file?  If so, can someone provide an example as to how I would do this and what Target I should be using.

2.  Is there a better, more appropriate method for doing this within the Zephyr environment.


Any help or guidance is much appreciated!