Skip to content

Commit 379afe7

Browse files
authored
Merge pull request #14953 from LDong-Arm/post_build_hook_rework
Rework post-build to support multiple executables
2 parents c8b889e + 23d659e commit 379afe7

File tree

14 files changed

+306
-219
lines changed

14 files changed

+306
-219
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: test building multiple executables with CMake
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
multiple-executables-example:
7+
runs-on: ubuntu-latest
8+
container: mbedos/mbed-os-env:latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
13+
- name: Build the multiple_executables example
14+
run: |
15+
mbedtools compile \
16+
-t GCC_ARM \
17+
-m ARM_MUSCA_S1 \
18+
--program-path tools/cmake/tests/multiple_executables/ \
19+
--mbed-os-path .
20+
21+
- name: Verify the post-build command has run successfully on each image
22+
run: |
23+
APP1=tools/cmake/tests/multiple_executables/cmake_build/ARM_MUSCA_S1/develop/GCC_ARM/app1/app1.bin
24+
APP2=tools/cmake/tests/multiple_executables/cmake_build/ARM_MUSCA_S1/develop/GCC_ARM/app2/app2.bin
25+
BOOTLOADER=targets/TARGET_ARM_SSG/TARGET_MUSCA_S1/bl2.bin
26+
BOOTLOADER_SIZE=`du -b targets/TARGET_ARM_SSG/TARGET_MUSCA_S1/bl2.bin | cut -f1`
27+
cmp -n $BOOTLOADER_SIZE $APP1 $BOOTLOADER
28+
cmp -n $BOOTLOADER_SIZE $APP2 $BOOTLOADER

CMakeLists.txt

Lines changed: 5 additions & 117 deletions
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)
@@ -171,122 +175,6 @@ if(${CMAKE_CROSSCOMPILING})
171175
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
172176
endif()
173177

174-
#
175-
# Converts output file of `target` to binary file and to Intel HEX file.
176-
#
177-
function(mbed_generate_bin_hex target)
178-
get_property(elf_to_bin GLOBAL PROPERTY ELF2BIN)
179-
if (MBED_TOOLCHAIN STREQUAL "GCC_ARM")
180-
# The first condition is quoted in case MBED_OUTPUT_EXT is unset
181-
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
182-
list(APPEND CMAKE_POST_BUILD_COMMAND
183-
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin
184-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
185-
)
186-
endif()
187-
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
188-
list(APPEND CMAKE_POST_BUILD_COMMAND
189-
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex
190-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
191-
)
192-
endif()
193-
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
194-
get_property(mbed_studio_arm_compiler GLOBAL PROPERTY MBED_STUDIO_ARM_COMPILER)
195-
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
196-
list(APPEND CMAKE_POST_BUILD_COMMAND
197-
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin $<TARGET_FILE:${target}>
198-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
199-
)
200-
endif()
201-
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
202-
list(APPEND CMAKE_POST_BUILD_COMMAND
203-
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --i32combined -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex $<TARGET_FILE:${target}>
204-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
205-
)
206-
endif()
207-
endif()
208-
add_custom_command(
209-
TARGET
210-
${target}
211-
POST_BUILD
212-
${CMAKE_POST_BUILD_COMMAND}
213-
COMMENT
214-
"executable:"
215-
VERBATIM
216-
)
217-
218-
if(TARGET mbed-post-build-bin-${MBED_TARGET})
219-
# Remove the .elf file to force regenerate the application binaries
220-
# (including .bin and .hex). This ensures that the post-build script runs
221-
# on a raw application instead of a previous build that already went
222-
# through the post-build process once.
223-
file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/${target}.elf)
224-
225-
# Pass the application's name to the Mbed target's post build operation
226-
set_target_properties(mbed-post-build-bin-${MBED_TARGET}
227-
PROPERTIES
228-
application ${target}
229-
)
230-
231-
# The artefacts must be created before they can be further manipulated
232-
add_dependencies(mbed-post-build-bin-${MBED_TARGET} ${target})
233-
234-
# Add a post-build hook to the top-level CMake target in the form of a
235-
# CMake custom target. The hook depends on Mbed target specific
236-
# post-build CMake target which has a custom command attached to it.
237-
add_custom_target(mbed-post-build ALL DEPENDS mbed-post-build-bin-${MBED_TARGET})
238-
endif()
239-
endfunction()
240-
241-
#
242-
# Parse toolchain generated map file of `target` and display a readable table format.
243-
#
244-
function(mbed_generate_map_file target)
245-
add_custom_command(
246-
TARGET
247-
${target}
248-
POST_BUILD
249-
COMMAND ${Python3_EXECUTABLE} ${mbed-os_SOURCE_DIR}/tools/memap.py -t ${MBED_TOOLCHAIN} ${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map
250-
WORKING_DIRECTORY
251-
${CMAKE_CURRENT_BINARY_DIR}
252-
COMMENT
253-
"Displaying memory map for ${target}"
254-
)
255-
endfunction()
256-
257-
#
258-
# Validate selected application profile.
259-
#
260-
function(mbed_validate_application_profile target)
261-
get_target_property(app_link_libraries ${target} LINK_LIBRARIES)
262-
string(FIND "${app_link_libraries}" "mbed-baremetal" string_found_position)
263-
if(${string_found_position} GREATER_EQUAL 0)
264-
if(NOT "bare-metal" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
265-
message(FATAL_ERROR
266-
"Use full profile as baremetal profile is not supported for this Mbed target")
267-
endif()
268-
elseif(NOT "full" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
269-
message(FATAL_ERROR
270-
"The full profile is not supported for this Mbed target")
271-
endif()
272-
endfunction()
273-
274-
#
275-
# Set post build operations
276-
#
277-
function(mbed_set_post_build target)
278-
# The mapfile name includes the top-level target name and the
279-
# executable suffix for all toolchains as CMake hardcodes the name of the
280-
# diagnostic output file for some toolchains.
281-
mbed_configure_memory_map(${target} "${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map")
282-
mbed_validate_application_profile(${target})
283-
mbed_generate_bin_hex(${target})
284-
285-
if(HAVE_MEMAP_DEPS)
286-
mbed_generate_map_file(${target})
287-
endif()
288-
endfunction()
289-
290178
# Ninja requires to be forced for response files
291179
if ("${CMAKE_GENERATOR}" MATCHES "Ninja")
292180
# known issue ARMClang and Ninja with response files for windows

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/TARGET_PSOC6/TARGET_CYSBSYSKIT_01/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,7 @@ target_compile_definitions(mbed-cysbsyskit-01
5959
"CY8C624AFNI_S2D43F"
6060
)
6161

62-
mbed_post_build_psoc6_merge_hex("CYSBSYSKIT_01")
62+
mbed_post_build_psoc6_merge_hex(
63+
PSOC6_TARGET
64+
"CYSBSYSKIT_01"
65+
)

targets/TARGET_Cypress/scripts/mbed_set_post_build_cypress.cmake

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,91 @@
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)
10+
set(prefix "CYPRESS")
11+
set(options)
12+
set(oneValueArgs
13+
PSOC6_TARGET
14+
CORTEX_M0_HEX
15+
)
16+
set(multiValueArgs)
17+
cmake_parse_arguments(
18+
"${prefix}"
19+
"${options}"
20+
"${oneValueArgs}"
21+
"${multipleValueArgs}"
22+
${ARGN}
23+
)
1324

14-
# Copy ${ARGN} to a variable first as it cannot be used directly with
15-
# the list() command
16-
set (extra_macro_args ${ARGN})
25+
if("${CYPRESS_PSOC6_TARGET}" STREQUAL "${MBED_TARGET}")
26+
function(mbed_post_build_function target)
27+
find_package(Python3)
28+
set(post_build_command
29+
${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PSOC6.py
30+
merge
31+
--elf $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.elf
32+
--m4hex $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.hex
33+
)
1734

18-
# Get the number of arguments past the last expected argument
19-
list(LENGTH extra_macro_args num_extra_args)
35+
if(NOT "${CYPRESS_CORTEX_M0_HEX}" STREQUAL "")
36+
list(APPEND post_build_command
37+
--m0hex ${CYPRESS_CORTEX_M0_HEX}
38+
)
39+
endif()
2040

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-
)
41+
add_custom_command(
42+
TARGET
43+
${target}
44+
POST_BUILD
45+
COMMAND
46+
${post_build_command}
47+
)
48+
endfunction()
3849
endif()
39-
40-
mbed_set_post_build_operation()
41-
endfunction()
50+
endmacro()
4251

4352

4453
#
4554
# Sign a Cortex-M4 HEX with Cortex-M0 HEX.
4655
#
47-
function(mbed_post_build_psoc6_sign_image
56+
macro(mbed_post_build_psoc6_sign_image
4857
m0hex_filename
49-
mbed_target_name
58+
cypress_psoc6_target
5059
policy_file_name
5160
boot_scheme
5261
cm0_img_id
5362
cm4_img_id
5463
cortex_m0_hex
5564
)
56-
find_package(Python3)
65+
if("${cypress_psoc6_target}" STREQUAL "${MBED_TARGET}")
66+
function(mbed_post_build_function target)
67+
find_package(Python3)
5768

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-
)
69+
set(post_build_command
70+
${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PSOC6.py
71+
sign
72+
--build-dir ${CMAKE_BINARY_DIR}
73+
--m0hex-filename ${m0hex_filename}
74+
--target-name ${cypress_psoc6_target}
75+
--policy-file-name ${policy_file_name}
76+
--boot-scheme ${boot_scheme}
77+
--cm0-img-id ${cm0_img_id}
78+
--cm4-img-id ${cm4_img_id}
79+
--elf $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.elf
80+
--m4hex $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.hex
81+
--m0hex ${cortex_m0_hex}
82+
)
7283

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

0 commit comments

Comments
 (0)