<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Automating merged.hex Patching After Build</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/121321/automating-merged-hex-patching-after-build</link><description>Hello Nordic team, 
 I would like to automatically patch the merged.hex file each time I build my project. I’ve attempted to use extra_post_build in CMakeLists.txt , but my patching script still runs before the merged.hex file is generated. 
 From what</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 21 Jul 2025 15:56:13 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/121321/automating-merged-hex-patching-after-build" /><item><title>RE: Automating merged.hex Patching After Build</title><link>https://devzone.nordicsemi.com/thread/543062?ContentTypeID=1</link><pubDate>Mon, 21 Jul 2025 15:56:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fecc794d-293f-4bb4-9d1d-e8663e6a06fd</guid><dc:creator>dubois</dc:creator><description>&lt;p&gt;Right, it was just a trivial example that shows how to create a target that depends on merged_hex.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automating merged.hex Patching After Build</title><link>https://devzone.nordicsemi.com/thread/543057?ContentTypeID=1</link><pubDate>Mon, 21 Jul 2025 15:36:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0c0bf212-e05a-42ae-b0fd-54971333abc7</guid><dc:creator>Glaskever</dc:creator><description>&lt;p&gt;&lt;span&gt;The &amp;quot;work around&amp;quot;&amp;nbsp;by creating a dummy.out and depending merged_hex on this dummy.out doesn&amp;#39;t seem to be necessary.&lt;br /&gt; &lt;br /&gt; The key solution (at least for me) seems that the custom commands, that need to run after merged.hex are created, are now located in sysbuild/CmakeLists.txt instead of in the projects CmakeLists.txt.&lt;br /&gt; &lt;br /&gt; My sysbuild/CmakeList has some more project specific names, but this should do the trick: &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;find_package(Sysbuild REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sysbuild LANGUAGES)

set(DIR ${CMAKE_CURRENT_SOURCE_DIR}/../)
set(BUILD_DIR ${DIR}/build)

add_custom_command(OUTPUT ${BUILD_DIR}/changed_merged.hex
    WORKING_DIRECTORY ${DIR}
    DEPENDS merged_hex
    COMMAND python ${DIR}/foo/bar.py ${BUILD_DIR}/merged.hex)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automating merged.hex Patching After Build</title><link>https://devzone.nordicsemi.com/thread/540356?ContentTypeID=1</link><pubDate>Tue, 24 Jun 2025 13:58:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5cbed7e-2c8d-47b1-a461-64f836556360</guid><dc:creator>dubois</dc:creator><description>&lt;p&gt;You can copy path/to/ncs/version/zephyr/share/sysbuild/template/CMakeLists.txt to your project&amp;#39;s sysbuild directory and then add your custom command and target there. For example:&lt;/p&gt;
&lt;pre&gt;$ cp ~/ncs/v2.8.0/zephyr/share/sysbuild/template/CMakeLists.txt myapp/sysbuild/&lt;br /&gt;$ nvim myapp/sysbuild/CMakeLists.txt&lt;br /&gt;$ cat myapp/sysbuild/CMakeLists.txt &lt;br /&gt;find_package(Sysbuild REQUIRED HINTS $ENV{ZEPHYR_BASE})&lt;br /&gt;project(sysbuild LANGUAGES)&lt;br /&gt;add_custom_command(OUTPUT dummy.out COMMAND cmake -E touch dummy.out)&lt;br /&gt;add_custom_target(did-it-work ALL DEPENDS dummy.out merged_hex)&lt;br /&gt;$&lt;/pre&gt;
&lt;pre&gt;[19/21] Generating ../dfu_application.zip&lt;br /&gt;[20/21] Generating ../merged.hex&lt;br /&gt;[21/21] Generating dummy.out&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automating merged.hex Patching After Build</title><link>https://devzone.nordicsemi.com/thread/535050?ContentTypeID=1</link><pubDate>Tue, 13 May 2025 08:42:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:290a137a-efdb-4dfc-9af8-fb7615fca1ac</guid><dc:creator>kelk</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The commad&amp;nbsp; is still being executed before the merged files are generated.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;[0/24] Performing build step for &amp;#39;ipc_radio&amp;#39;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;ninja: no work to do.&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;[2/7] Linking C static library app\libapp.a&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#ff0000;"&gt;&lt;span style="text-decoration:underline;"&gt;Running post-build patch on merged.hex..&lt;/span&gt;.&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;[7/7] Linking C executable zephyr\zephyr.elf&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;Memory region Used Size Region Size %age Used&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; FLASH: 516152 B 916992 B 56.29%&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; RAM: 243496 B 450544 B 54.04%&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; IDT_LIST: 0 GB 32 KB 0.00%&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;Generating files from C:/projects/sw_b/build/sw_b/zephyr/zephyr.elf for board: cpn0003&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;image.py: sign the digest&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;image.py: sign the digest&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;[5/20] Performing build step for &amp;#39;mcuboot&amp;#39;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;ninja: no work to do.&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;[8/20] Performing build step for &amp;#39;b0n&amp;#39;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;ninja: no work to do.&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#ff0000;text-decoration:underline;"&gt;[18/18] Generating ../merged.hex&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt; * Terminal will be reused by tasks, press any key to close it. &lt;/strong&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automating merged.hex Patching After Build</title><link>https://devzone.nordicsemi.com/thread/535004?ContentTypeID=1</link><pubDate>Tue, 13 May 2025 05:42:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e3b06c61-0662-43d5-bd97-8f2faa274ffa</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Sorry for that experience.&lt;/p&gt;
&lt;p&gt;Can you please make sure that you have enabled the MCUBoot since this triggers the merged output via imgtool . I tested with this below&amp;nbsp;&lt;/p&gt;
&lt;p&gt;my testing scripts/patch_hex.py contains (you might have different patch_hex.py)&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;import sys
print(f&amp;quot;Patching {sys.argv[1]} ...&amp;quot;)
with open(sys.argv[1], &amp;quot;a&amp;quot;) as f:
    f.write(&amp;quot;\n# Patched!\n&amp;quot;)
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Adding below to the CMakeLists.txt&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;# Run patching script after merged.hex is fully generated
if(CONFIG_BOOTLOADER_MCUBOOT)
  add_custom_command(
    TARGET app
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E echo &amp;quot;Running post-build patch on merged.hex...&amp;quot;
    COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/patch_hex.py
            ${CMAKE_BINARY_DIR}/zephyr/merged.hex
    COMMENT &amp;quot;Post-build: patch_hex.py running after merged.hex is created&amp;quot;
    VERBATIM
  )
endif()
&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Make sure you do not have any typos. and make sure that&amp;nbsp;CMAKE_CURRENT_SOURCE_DIR and&amp;nbsp;&amp;nbsp;${CMAKE_BINARY_DIR}/zephyr/merged.hex exists or replace them with the right directory.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;west build -b nrf5340dk/nrf5340/cpuapp -p&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;should give something like&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;Running post-build patch on merged.hex...
Patching build/zephyr/merged.hex ...&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automating merged.hex Patching After Build</title><link>https://devzone.nordicsemi.com/thread/534897?ContentTypeID=1</link><pubDate>Mon, 12 May 2025 12:45:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:65e20eae-762a-46d8-8b4a-5a1bbb72d621</guid><dc:creator>kelk</dc:creator><description>&lt;div&gt;
&lt;p class="" data-start="78" data-end="81"&gt;Hi,&lt;/p&gt;
&lt;p class="" data-start="83" data-end="147"&gt;I tested the provided code, but encountered the following error:&lt;/p&gt;
&lt;p class="" data-start="83" data-end="147"&gt;&lt;pre class="ui-code" data-mode="text"&gt;CMake Error at CMakeLists.txt:119 (add_custom_command):
  No TARGET &amp;#39;mergehex&amp;#39; has been created in this directory.
&lt;/pre&gt;&lt;/p&gt;
&lt;p class="" data-start="83" data-end="147"&gt;To work around this, I manually added the target using:&lt;/p&gt;
&lt;p class="" data-start="83" data-end="147"&gt;&lt;pre class="ui-code" data-mode="text"&gt;add_custom_target(mergehex
--input ${CMAKE_BINARY_DIR}/../merged.hex
)&lt;/pre&gt;&lt;/p&gt;
&lt;p class="" data-start="83" data-end="147"&gt;However, after doing so, the Python script does not get executed.&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automating merged.hex Patching After Build</title><link>https://devzone.nordicsemi.com/thread/534596?ContentTypeID=1</link><pubDate>Fri, 09 May 2025 08:54:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:223013c1-82b7-4cb6-94a7-658541914f9d</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Add a condition in your CMakeLists.txt using POST_BUILD for your execution something like below&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;if (DEFINED CONFIG_BOOTLOADER_MCUBOOT)
  add_custom_command(
    TARGET mergehex
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${CMAKE_CURRENT_SOURCE_DIR}
            python ${CMAKE_CURRENT_SOURCE_DIR}/scripts/patch_hex.py
            ${CMAKE_BINARY_DIR}/merged.hex
  )
endif()
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>