How does partition_manager.cmake get invoked such that its mergehex call is always the last step?

NCS 2.9.1

NRF52840

Sysbuild but with only a single image (no bootloader)

I've been trying to figure out how to change the merged.hex output and how to perform additional steps after the merged.hex is generated.

I was finally able to track down the generation of merged.hex to the partition_manager function in nrf/cmake/sysbulid/partition_manager.cmake  I'm able to change the name of the merged.hex file by modifying the variable ${merged}  in this code:

set(merged zephyr-merged${merged_suffix})
set(MERGED ZEPHYR-MERGED${MERGED_SUFFIX})

Within the cmake function partition_manager, this command is added:

add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/${container}.hex
COMMAND
${PYTHON_EXECUTABLE}
${ZEPHYR_BASE}/scripts/build/mergehex.py
-o ${CMAKE_BINARY_DIR}/${container}.hex
${${container}overlap_arg}
${${container}hex_files}
DEPENDS
${${container}targets}
${${container}hex_files}
)

Which has a corresponding custom target

add_custom_target(
${container}_hex
ALL DEPENDS
${CMAKE_BINARY_DIR}/${container}.hex
)

However I'm still confused by how these partition_manager targets are not usable from my project's CMakeLists.txt.  For example how can I make an additional custom target that depends on merged_hex.   Is this a different cmake run after the POST_BUILD that doesn't include the app CMakeLists.txt?

As well, there is this code which may be unrelated, but I'm not sure what it does:

if ("${container}" STREQUAL "merged")
update_runner(IMAGE ${DEFAULT_IMAGE} HEX ${CMAKE_BINARY_DIR}/${container}.hex)
endif()

Related