Skip to content

Commit e0b103d

Browse files
committed
Rework post-build to support multiple executables
When building greentea tests, each test is an executable with its own output binary path. This is also the case when a user project produces multiple executables. But the current implementation of post-build operations always assumes there's only one executable, at the root of the build directory. The post-build command depends on Mbed target, and it always takes the the executable we build as an input file. To achieve this, we let each Mbed target (that has a post-build command) define a function function(mbed_post_build_function target) which takes a CMake executable target as an argument from which it can get its binary path using generator expressions. It generates and adds to the passed executable target a post-build custom command. Notes: * The function name needs to be exact, because CMake only supports literal function calls - CMake can't dereference a function name from a variable. To avoid multiple definitions of this function, each Mbed target needs to guard it with a macro to checks if the user is building this Mbed target. * `mbed_post_build_function()` is a function, but it is usually defined by another macro rather than a parent function, because nesting functions would make many variables inaccessible inside the innermost `mbed_post_build_function()`. * There's no more need to force regenerate images. Previously, post- build commands were custom *targets* which always got to run, so we force regenerated images on every build to avoid patching an image that's already been patched once on previous build. Now post-build commands are custom *commands* of the same executable target, and they are only run if the executable target itself is rebuilt.
1 parent fcd57b2 commit e0b103d

File tree

6 files changed

+131
-134
lines changed

6 files changed

+131
-134
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,16 @@ target_include_directories(mbed-core
144144
add_library(mbed-device_key INTERFACE)
145145
add_library(mbed-rtos INTERFACE)
146146

147+
# Include targets/ first, because any post-build hook needs to be defined
148+
# before other parts of Mbed OS can add greentea tests which require
149+
# mbed_set_post_build().
150+
add_subdirectory(targets)
151+
147152
add_subdirectory(cmsis)
148153
add_subdirectory(drivers)
149154
add_subdirectory(hal)
150155
add_subdirectory(platform)
151156
add_subdirectory(rtos)
152-
add_subdirectory(targets)
153157
add_subdirectory(storage)
154158
add_subdirectory(events)
155159
add_subdirectory(connectivity)

platform/FEATURE_EXPERIMENTAL_API/FEATURE_PSA/TARGET_TFM/TARGET_TFM_LATEST/scripts/mbed_set_post_build_tfm.cmake

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,30 @@
33

44
include(mbed_set_post_build)
55

6-
set(MBED_POST_BUILD_TFM_DIR "${CMAKE_CURRENT_LIST_DIR}")
7-
86
#
97
# Sign TF-M secure and non-secure images and combine them with the bootloader
108
#
11-
function(mbed_post_build_tfm_sign_image
12-
mbed_target
9+
macro(mbed_post_build_tfm_sign_image
10+
mbed_tfm_target
1311
tfm_target
1412
target_path
1513
secure_bin
1614
)
17-
find_package(Python3)
18-
19-
set(mbed_target_name ${mbed_target})
20-
set(post_build_command
21-
COMMAND ${Python3_EXECUTABLE}
22-
${MBED_POST_BUILD_TFM_DIR}/generate_mbed_image.py
23-
--tfm-target ${tfm_target}
24-
--target-path ${target_path}
25-
--secure-bin ${secure_bin}
26-
--non-secure-bin ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.bin
27-
)
28-
29-
mbed_set_post_build_operation()
30-
endfunction()
15+
if("${mbed_tfm_target}" STREQUAL "${MBED_TARGET}")
16+
function(mbed_post_build_function target)
17+
find_package(Python3)
18+
add_custom_command(
19+
TARGET
20+
${target}
21+
POST_BUILD
22+
COMMAND
23+
${Python3_EXECUTABLE}
24+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/generate_mbed_image.py
25+
--tfm-target ${tfm_target}
26+
--target-path ${target_path}
27+
--secure-bin ${secure_bin}
28+
--non-secure-bin $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.bin
29+
)
30+
endfunction()
31+
endif()
32+
endmacro()

targets/TARGET_Cypress/scripts/mbed_set_post_build_cypress.cmake

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,90 @@
33

44
include(mbed_set_post_build)
55

6-
set(MBED_POST_BUILD_CYPRESS_DIR "${CMAKE_CURRENT_LIST_DIR}")
7-
86
#
97
# Merge Cortex-M4 HEX and a Cortex-M0 HEX.
108
#
11-
function(mbed_post_build_psoc6_merge_hex mbed_target_name)
12-
find_package(Python3)
9+
macro(mbed_post_build_psoc6_merge_hex cypress_psoc6_target)
10+
if("${cypress_psoc6_target}" STREQUAL "${MBED_TARGET}")
11+
function(mbed_post_build_function target)
12+
find_package(Python3)
1313

14-
# Copy ${ARGN} to a variable first as it cannot be used directly with
15-
# the list() command
16-
set (extra_macro_args ${ARGN})
14+
# Copy ${ARGN} to a variable first as it cannot be used directly with
15+
# the list() command
16+
set (extra_macro_args ${ARGN})
1717

18-
# Get the number of arguments past the last expected argument
19-
list(LENGTH extra_macro_args num_extra_args)
18+
# Get the number of arguments past the last expected argument
19+
list(LENGTH extra_macro_args num_extra_args)
2020

21-
if(${num_extra_args} GREATER 0)
22-
# Get extra argument as `cortex_m0_hex`
23-
list(GET extra_macro_args 0 cortex_m0_hex)
24-
set(post_build_command
25-
COMMAND ${Python3_EXECUTABLE} ${MBED_POST_BUILD_CYPRESS_DIR}/PSOC6.py
26-
merge
27-
--elf ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.elf
28-
--m4hex ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.hex
29-
--m0hex ${cortex_m0_hex}
30-
)
31-
else()
32-
set(post_build_command
33-
COMMAND ${Python3_EXECUTABLE} ${MBED_POST_BUILD_CYPRESS_DIR}/PSOC6.py
34-
merge
35-
--elf ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.elf
36-
--m4hex ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.hex
37-
)
38-
endif()
21+
if(${num_extra_args} GREATER 0)
22+
# Get extra argument as `cortex_m0_hex`
23+
list(GET extra_macro_args 0 cortex_m0_hex)
24+
set(post_build_command
25+
${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PSOC6.py
26+
merge
27+
--elf $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.elf
28+
--m4hex $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.hex
29+
--m0hex ${cortex_m0_hex}
30+
)
31+
else()
32+
set(post_build_command
33+
${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PSOC6.py
34+
merge
35+
--elf $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.elf
36+
--m4hex $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.hex
37+
)
38+
endif()
3939

40-
mbed_set_post_build_operation()
41-
endfunction()
40+
add_custom_command(
41+
TARGET
42+
${target}
43+
POST_BUILD
44+
COMMAND
45+
${post_build_command}
46+
)
47+
endfunction()
48+
endif()
49+
endmacro()
4250

4351

4452
#
4553
# Sign a Cortex-M4 HEX with Cortex-M0 HEX.
4654
#
47-
function(mbed_post_build_psoc6_sign_image
55+
macro(mbed_post_build_psoc6_sign_image
4856
m0hex_filename
49-
mbed_target_name
57+
cypress_psoc6_target
5058
policy_file_name
5159
boot_scheme
5260
cm0_img_id
5361
cm4_img_id
5462
cortex_m0_hex
5563
)
56-
find_package(Python3)
64+
if("${cypress_psoc6_target}" STREQUAL "${MBED_TARGET}")
65+
function(mbed_post_build_function target)
66+
find_package(Python3)
5767

58-
set(post_build_command
59-
COMMAND ${Python3_EXECUTABLE} ${MBED_POST_BUILD_CYPRESS_DIR}/PSOC6.py
60-
sign
61-
--build-dir ${CMAKE_BINARY_DIR}
62-
--m0hex-filename ${m0hex_filename}
63-
--target-name ${mbed_target_name}
64-
--policy-file-name ${policy_file_name}
65-
--boot-scheme ${boot_scheme}
66-
--cm0-img-id ${cm0_img_id}
67-
--cm4-img-id ${cm4_img_id}
68-
--elf ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.elf
69-
--m4hex ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.hex
70-
--m0hex ${cortex_m0_hex}
71-
)
68+
set(post_build_command
69+
${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PSOC6.py
70+
sign
71+
--build-dir ${CMAKE_BINARY_DIR}
72+
--m0hex-filename ${m0hex_filename}
73+
--target-name ${cypress_psoc6_target}
74+
--policy-file-name ${policy_file_name}
75+
--boot-scheme ${boot_scheme}
76+
--cm0-img-id ${cm0_img_id}
77+
--cm4-img-id ${cm4_img_id}
78+
--elf $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.elf
79+
--m4hex $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.hex
80+
--m0hex ${cortex_m0_hex}
81+
)
7282

73-
mbed_set_post_build_operation()
74-
endfunction()
83+
add_custom_command(
84+
TARGET
85+
${target}
86+
POST_BUILD
87+
COMMAND
88+
${post_build_command}
89+
)
90+
endfunction()
91+
endif()
92+
endmacro()

targets/TARGET_NUVOTON/scripts/mbed_set_post_build_nuvoton.cmake

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,27 @@ include(${MBED_PATH}/tools/cmake/mbed_set_post_build.cmake)
66
#
77
# Sign TF-M secure and non-secure images and combine them with the bootloader
88
#
9-
function(mbed_post_build_nuvoton_tfm_sign_image_tgt
10-
mbed_target
9+
macro(mbed_post_build_nuvoton_tfm_sign_image_tgt
10+
nuvoton_target
1111
tfm_import_path
1212
signing_key
1313
)
14-
find_package(Python3)
14+
if("${nuvoton_target}" STREQUAL "${MBED_TARGET}")
15+
function(mbed_post_build_function target)
16+
find_package(Python3)
1517

16-
set(mbed_target_name ${mbed_target})
17-
set(post_build_command
18-
COMMAND ${Python3_EXECUTABLE}
19-
${MBED_PATH}/targets/TARGET_NUVOTON/scripts/NUVOTON.py
20-
tfm_sign_image_tgt
21-
--tfm-import-path ${tfm_import_path}
22-
--signing_key ${signing_key}
23-
--non-secure-bin ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.bin
24-
)
25-
26-
mbed_set_post_build_operation()
27-
endfunction()
18+
add_custom_command(
19+
TARGET
20+
${target}
21+
POST_BUILD
22+
COMMAND
23+
${Python3_EXECUTABLE}
24+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/NUVOTON.py
25+
tfm_sign_image_tgt
26+
--tfm-import-path ${tfm_import_path}
27+
--signing_key ${signing_key}
28+
--non-secure-bin $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.bin
29+
)
30+
endfunction()
31+
endif()
32+
endmacro()

targets/TARGET_NXP/scripts/mbed_set_post_build_nxp.cmake

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33

44
include(mbed_set_post_build)
55

6-
set(MBED_POST_BUILD_NXP_DIR "${CMAKE_CURRENT_LIST_DIR}")
7-
86
#
97
# Patch an LPC target vector table in the binary file.
108
#
11-
function(mbed_post_build_lpc_patch_vtable mbed_target_name)
12-
find_package(Python3)
13-
14-
set(post_build_command
15-
COMMAND ${Python3_EXECUTABLE} ${MBED_POST_BUILD_NXP_DIR}/LPC.py
16-
${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.bin
17-
)
18-
19-
mbed_set_post_build_operation()
20-
endfunction()
9+
macro(mbed_post_build_lpc_patch_vtable nxp_lpc_target)
10+
if("${nxp_lpc_target}" STREQUAL "${MBED_TARGET}")
11+
function(mbed_post_build_function target)
12+
find_package(Python3)
13+
add_custom_command(
14+
TARGET
15+
${target}
16+
POST_BUILD
17+
COMMAND
18+
${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/LPC.py
19+
$<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.bin
20+
)
21+
endfunction()
22+
endif()
23+
endmacro()

tools/cmake/mbed_set_post_build.cmake

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,12 @@ function(mbed_generate_bin_hex target)
3939
TARGET
4040
${target}
4141
POST_BUILD
42+
COMMAND
4243
${CMAKE_POST_BUILD_COMMAND}
4344
COMMENT
4445
"executable:"
4546
VERBATIM
4647
)
47-
48-
if(TARGET mbed-post-build-bin-${MBED_TARGET})
49-
# Remove the .elf file to force regenerate the application binaries
50-
# (including .bin and .hex). This ensures that the post-build script runs
51-
# on a raw application instead of a previous build that already went
52-
# through the post-build process once.
53-
file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/${target}.elf)
54-
55-
# Pass the application's name to the Mbed target's post build operation
56-
set_target_properties(mbed-post-build-bin-${MBED_TARGET}
57-
PROPERTIES
58-
application ${target}
59-
)
60-
61-
# The artefacts must be created before they can be further manipulated
62-
add_dependencies(mbed-post-build-bin-${MBED_TARGET} ${target})
63-
64-
# Add a post-build hook to the top-level CMake target in the form of a
65-
# CMake custom target. The hook depends on Mbed target specific
66-
# post-build CMake target which has a custom command attached to it.
67-
add_custom_target(mbed-post-build ALL DEPENDS mbed-post-build-bin-${MBED_TARGET})
68-
endif()
6948
endfunction()
7049

7150
#
@@ -112,25 +91,11 @@ function(mbed_set_post_build target)
11291
mbed_validate_application_profile(${target})
11392
mbed_generate_bin_hex(${target})
11493

94+
if(COMMAND mbed_post_build_function)
95+
mbed_post_build_function(${target})
96+
endif()
97+
11598
if(HAVE_MEMAP_DEPS)
11699
mbed_generate_map_file(${target})
117100
endif()
118101
endfunction()
119-
120-
121-
#
122-
# Sets the post build operation for Mbed targets.
123-
#
124-
macro(mbed_set_post_build_operation)
125-
126-
add_custom_target(mbed-post-build-bin-${mbed_target_name})
127-
128-
add_custom_command(
129-
TARGET
130-
mbed-post-build-bin-${mbed_target_name}
131-
POST_BUILD
132-
COMMAND
133-
${post_build_command}
134-
VERBATIM
135-
)
136-
endmacro()

0 commit comments

Comments
 (0)