From 3579bb65da66f1dd0572ecbecfd670a978590f57 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Wed, 3 Mar 2021 18:11:45 +0000 Subject: [PATCH 1/4] Force regenerate application binaries on rebuild If a target has a post binary hook, we should force regenerate the application binaries every time so that the hook is run on a raw binary rather than a previous build that already went through the post-build process once. --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index fe0587aec2..f5857f3d94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,6 +171,12 @@ function(mbed_generate_bin_hex target) ) if(TARGET mbed-post-build-bin-${MBED_TARGET}) + # Remove the .elf file to force regenerate the application binaries + # (including .bin and .hex). This ensures that the post-build script runs + # on a raw application instead of a previous build that already went + # through the post-build process once. + file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/${target}.elf) + # The artefacts must be created before they can be further manipulated add_dependencies(mbed-post-build-bin-${MBED_TARGET} ${target}) From ad27a3ec6993f474ab96e373874f12e6577be6be Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Wed, 3 Mar 2021 18:26:11 +0000 Subject: [PATCH 2/4] CMake: pass application name to post-build hook Pass the exact name to the post-build hook, when an application's or test's `CMakeLists.txt` calls `mbed_set_post_build()`. The interface is CMake's built-in mechanism: * `set_target_properties()` to set a property, e.g. application name * `$` to query a property at run time --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5857f3d94..ad61efd96b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,6 +177,12 @@ function(mbed_generate_bin_hex target) # through the post-build process once. file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/${target}.elf) + # Pass the application's name to the Mbed target's post build operation + set_target_properties(mbed-post-build-bin-${MBED_TARGET} + PROPERTIES + application ${target} + ) + # The artefacts must be created before they can be further manipulated add_dependencies(mbed-post-build-bin-${MBED_TARGET} ${target}) From 5353b628736fae329aa986c1c6203156bb4f1d18 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Wed, 3 Mar 2021 18:43:58 +0000 Subject: [PATCH 3/4] Cypress: Query exact application names in post build This removes the need to use "glob" a file extension, and uses precise application names. --- targets/TARGET_Cypress/scripts/PSOC6.py | 32 +++++++------------ .../scripts/mbed_set_post_build_cypress.cmake | 9 ++++-- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/targets/TARGET_Cypress/scripts/PSOC6.py b/targets/TARGET_Cypress/scripts/PSOC6.py index e9b299daf5..ea0d91a43e 100644 --- a/targets/TARGET_Cypress/scripts/PSOC6.py +++ b/targets/TARGET_Cypress/scripts/PSOC6.py @@ -318,29 +318,13 @@ def complete(message_func, elf0, hexf0, hexf1=None): def merge_action(args): """Entry point for the "merge" CLI command.""" - try: - elf_file = list(pathlib.Path(args.artefacts_location).glob("*.elf"))[0] - m4hex_file = list(pathlib.Path(args.artefacts_location).glob("*.hex"))[0] - except IndexError: - raise ArtefactsError( - f"Could not find elf and/or hex file in {args.artefacts_location}" - ) - complete_func( - print, elf_file, m4hex_file, args.m0hex + print, args.elf, args.m4hex, args.m0hex ) def sign_action(args): """Entry point for the "sign" CLI command.""" - try: - elf_file = list(pathlib.Path(args.artefacts_location).glob("*.elf"))[0] - m4hex_file = list(pathlib.Path(args.artefacts_location).glob("*.hex"))[0] - except IndexError: - raise ArtefactsError( - f"Could not find elf and/or hex file in {args.artefacts_location}" - ) - sign_hex( args.build_dir, args.m0hex_filename, @@ -350,8 +334,8 @@ def sign_action(args): args.boot_scheme, args.cm0_img_id, args.cm4_img_id, - elf_file, - m4hex_file, + args.elf, + args.m4hex, args.m0hex ) @@ -368,7 +352,10 @@ def parse_args(): "merge", help="Merge Cortex-M4 and Cortex-M0 HEX files." ) merge_subcommand.add_argument( - "--artefacts-location", required=True, help="the path to the application artefacts." + "--elf", required=True, help="the application ELF file." + ) + merge_subcommand.add_argument( + "--m4hex", required=True, help="the path to the Cortex-M4 HEX to merge." ) merge_subcommand.add_argument( "--m0hex", help="the path to the Cortex-M0 HEX to merge." @@ -400,7 +387,10 @@ def parse_args(): "--cm4-img-id", type=int, help="the Cortex-M4 image ID." ) sign_subcommand.add_argument( - "--artefacts-location", required=True, help="the path to the application artefacts." + "--elf", required=True, help="the application ELF file." + ) + sign_subcommand.add_argument( + "--m4hex", required=True, help="the path to the Cortex-M4 HEX to merge." ) sign_subcommand.add_argument( "--m0hex", help="the path to the Cortex-M0 HEX to merge." diff --git a/targets/TARGET_Cypress/scripts/mbed_set_post_build_cypress.cmake b/targets/TARGET_Cypress/scripts/mbed_set_post_build_cypress.cmake index 01e950699e..399900d5a4 100644 --- a/targets/TARGET_Cypress/scripts/mbed_set_post_build_cypress.cmake +++ b/targets/TARGET_Cypress/scripts/mbed_set_post_build_cypress.cmake @@ -22,14 +22,16 @@ function(mbed_post_build_psoc6_merge_hex mbed_target_name) set(post_build_command COMMAND ${Python3_EXECUTABLE} ${MBED_PATH}/targets/TARGET_Cypress/scripts/PSOC6.py merge - --artefacts-location ${CMAKE_BINARY_DIR} + --elf ${CMAKE_BINARY_DIR}/$.elf + --m4hex ${CMAKE_BINARY_DIR}/$.hex --m0hex ${cortex_m0_hex} ) else() set(post_build_command COMMAND ${Python3_EXECUTABLE} ${MBED_PATH}/targets/TARGET_Cypress/scripts/PSOC6.py merge - --artefacts-location ${CMAKE_BINARY_DIR} + --elf ${CMAKE_BINARY_DIR}/$.elf + --m4hex ${CMAKE_BINARY_DIR}/$.hex ) endif() @@ -61,7 +63,8 @@ function(mbed_post_build_psoc6_sign_image --boot-scheme ${boot_scheme} --cm0-img-id ${cm0_img_id} --cm4-img-id ${cm4_img_id} - --artefacts-location ${CMAKE_BINARY_DIR} + --elf ${CMAKE_BINARY_DIR}/$.elf + --m4hex ${CMAKE_BINARY_DIR}/$.hex --m0hex ${cortex_m0_hex} ) From 848341112e3aee201d984b4a4b6a902864597548 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Wed, 3 Mar 2021 18:51:20 +0000 Subject: [PATCH 4/4] NXP: Query precise application names in post build This removes the need to use "glob" a file extension, and uses precise application names. --- targets/TARGET_NXP/scripts/LPC.py | 10 +++------- .../TARGET_NXP/scripts/mbed_set_post_build_nxp.cmake | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/targets/TARGET_NXP/scripts/LPC.py b/targets/TARGET_NXP/scripts/LPC.py index 095b35cf28..9ef8615061 100644 --- a/targets/TARGET_NXP/scripts/LPC.py +++ b/targets/TARGET_NXP/scripts/LPC.py @@ -24,7 +24,6 @@ causes the checksum of the first 8 table entries to be 0. The boot loader code c the first 8 locations in sector 0 of the flash. If the result is 0, then execution control is transferred to the user code. """ -import pathlib import os import sys from struct import unpack, pack @@ -53,13 +52,10 @@ def is_patched(bin_path): if __name__ == "__main__": - artefacts_location = sys.argv[1] - - try: - binary = list(pathlib.Path(artefacts_location).glob("*.bin"))[0] - except IndexError: + binary = sys.argv[1] + if not os.path.isfile(binary): raise ArtefactsError( - f"Could not find binary file in {artefacts_location}" + f"Could not find binary file {binary}" ) print("LPC Patch: %s" % os.path.split(binary)[1]) diff --git a/targets/TARGET_NXP/scripts/mbed_set_post_build_nxp.cmake b/targets/TARGET_NXP/scripts/mbed_set_post_build_nxp.cmake index 9a63433250..8e18e3956b 100644 --- a/targets/TARGET_NXP/scripts/mbed_set_post_build_nxp.cmake +++ b/targets/TARGET_NXP/scripts/mbed_set_post_build_nxp.cmake @@ -11,7 +11,7 @@ function(mbed_post_build_lpc_patch_vtable mbed_target_name) set(post_build_command COMMAND ${Python3_EXECUTABLE} ${MBED_PATH}/targets/TARGET_NXP/scripts/LPC.py - ${CMAKE_BINARY_DIR} + ${CMAKE_BINARY_DIR}/$.bin ) mbed_set_post_build_operation()