Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework post-build to support multiple executables #14953

Merged
merged 4 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 5 additions & 117 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,16 @@ target_include_directories(mbed-core
add_library(mbed-device_key INTERFACE)
add_library(mbed-rtos INTERFACE)

# Include targets/ first, because any post-build hook needs to be defined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the commit message for commit "Rework post-build to support multiple executables", we say "guard it with a macro to checks if", that should be "guard it with a macro to check if"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I probably forgot to change it when rephrasing the sentence. 😅

# before other parts of Mbed OS can add greentea tests which require
# mbed_set_post_build().
add_subdirectory(targets)

add_subdirectory(cmsis)
add_subdirectory(drivers)
add_subdirectory(hal)
add_subdirectory(platform)
add_subdirectory(rtos)
add_subdirectory(targets)
add_subdirectory(storage)
add_subdirectory(events)
add_subdirectory(connectivity)
Expand All @@ -171,122 +175,6 @@ if(${CMAKE_CROSSCOMPILING})
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
endif()

#
# Converts output file of `target` to binary file and to Intel HEX file.
#
function(mbed_generate_bin_hex target)
get_property(elf_to_bin GLOBAL PROPERTY ELF2BIN)
if (MBED_TOOLCHAIN STREQUAL "GCC_ARM")
# The first condition is quoted in case MBED_OUTPUT_EXT is unset
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
list(APPEND CMAKE_POST_BUILD_COMMAND
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
)
endif()
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
list(APPEND CMAKE_POST_BUILD_COMMAND
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
)
endif()
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
get_property(mbed_studio_arm_compiler GLOBAL PROPERTY MBED_STUDIO_ARM_COMPILER)
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
list(APPEND CMAKE_POST_BUILD_COMMAND
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin $<TARGET_FILE:${target}>
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
)
endif()
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
list(APPEND CMAKE_POST_BUILD_COMMAND
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --i32combined -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex $<TARGET_FILE:${target}>
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
)
endif()
endif()
add_custom_command(
TARGET
${target}
POST_BUILD
${CMAKE_POST_BUILD_COMMAND}
COMMENT
"executable:"
VERBATIM
)

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)

# 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})

# Add a post-build hook to the top-level CMake target in the form of a
# CMake custom target. The hook depends on Mbed target specific
# post-build CMake target which has a custom command attached to it.
add_custom_target(mbed-post-build ALL DEPENDS mbed-post-build-bin-${MBED_TARGET})
endif()
endfunction()

#
# Parse toolchain generated map file of `target` and display a readable table format.
#
function(mbed_generate_map_file target)
add_custom_command(
TARGET
${target}
POST_BUILD
COMMAND ${Python3_EXECUTABLE} ${mbed-os_SOURCE_DIR}/tools/memap.py -t ${MBED_TOOLCHAIN} ${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
COMMENT
"Displaying memory map for ${target}"
)
endfunction()

#
# Validate selected application profile.
#
function(mbed_validate_application_profile target)
get_target_property(app_link_libraries ${target} LINK_LIBRARIES)
string(FIND "${app_link_libraries}" "mbed-baremetal" string_found_position)
if(${string_found_position} GREATER_EQUAL 0)
if(NOT "bare-metal" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
message(FATAL_ERROR
"Use full profile as baremetal profile is not supported for this Mbed target")
endif()
elseif(NOT "full" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
message(FATAL_ERROR
"The full profile is not supported for this Mbed target")
endif()
endfunction()

#
# Set post build operations
#
function(mbed_set_post_build target)
# The mapfile name includes the top-level target name and the
# executable suffix for all toolchains as CMake hardcodes the name of the
# diagnostic output file for some toolchains.
mbed_configure_memory_map(${target} "${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map")
mbed_validate_application_profile(${target})
mbed_generate_bin_hex(${target})

if(HAVE_MEMAP_DEPS)
mbed_generate_map_file(${target})
endif()
endfunction()

# Ninja requires to be forced for response files
if ("${CMAKE_GENERATOR}" MATCHES "Ninja")
# known issue ARMClang and Ninja with response files for windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@

include(mbed_set_post_build)

set(MBED_POST_BUILD_TFM_DIR "${CMAKE_CURRENT_LIST_DIR}")

#
# Sign TF-M secure and non-secure images and combine them with the bootloader
#
function(mbed_post_build_tfm_sign_image
mbed_target
macro(mbed_post_build_tfm_sign_image
mbed_tfm_target
tfm_target
target_path
secure_bin
)
find_package(Python3)

set(mbed_target_name ${mbed_target})
set(post_build_command
COMMAND ${Python3_EXECUTABLE}
${MBED_POST_BUILD_TFM_DIR}/generate_mbed_image.py
--tfm-target ${tfm_target}
--target-path ${target_path}
--secure-bin ${secure_bin}
--non-secure-bin ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.bin
)

mbed_set_post_build_operation()
endfunction()
if("${mbed_tfm_target}" STREQUAL "${MBED_TARGET}")
function(mbed_post_build_function target)
find_package(Python3)
add_custom_command(
TARGET
${target}
POST_BUILD
COMMAND
${Python3_EXECUTABLE}
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/generate_mbed_image.py
--tfm-target ${tfm_target}
--target-path ${target_path}
--secure-bin ${secure_bin}
--non-secure-bin $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.bin
)
endfunction()
endif()
endmacro()
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ target_compile_definitions(mbed-cysbsyskit-01
"CY8C624AFNI_S2D43F"
)

mbed_post_build_psoc6_merge_hex("CYSBSYSKIT_01")
mbed_post_build_psoc6_merge_hex(
PSOC6_TARGET
"CYSBSYSKIT_01"
)
115 changes: 67 additions & 48 deletions targets/TARGET_Cypress/scripts/mbed_set_post_build_cypress.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,91 @@

include(mbed_set_post_build)

set(MBED_POST_BUILD_CYPRESS_DIR "${CMAKE_CURRENT_LIST_DIR}")

#
# Merge Cortex-M4 HEX and a Cortex-M0 HEX.
#
function(mbed_post_build_psoc6_merge_hex mbed_target_name)
find_package(Python3)
macro(mbed_post_build_psoc6_merge_hex)
set(prefix "CYPRESS")
set(options)
set(oneValueArgs
PSOC6_TARGET
CORTEX_M0_HEX
)
set(multiValueArgs)
cmake_parse_arguments(
"${prefix}"
"${options}"
"${oneValueArgs}"
"${multipleValueArgs}"
${ARGN}
)

# Copy ${ARGN} to a variable first as it cannot be used directly with
# the list() command
set (extra_macro_args ${ARGN})
if("${CYPRESS_PSOC6_TARGET}" STREQUAL "${MBED_TARGET}")
function(mbed_post_build_function target)
find_package(Python3)
set(post_build_command
${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PSOC6.py
merge
--elf $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.elf
--m4hex $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.hex
)

# Get the number of arguments past the last expected argument
list(LENGTH extra_macro_args num_extra_args)
if(NOT "${CYPRESS_CORTEX_M0_HEX}" STREQUAL "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better to me, the intent is now clear.

list(APPEND post_build_command
--m0hex ${CYPRESS_CORTEX_M0_HEX}
)
endif()

if(${num_extra_args} GREATER 0)
# Get extra argument as `cortex_m0_hex`
list(GET extra_macro_args 0 cortex_m0_hex)
set(post_build_command
COMMAND ${Python3_EXECUTABLE} ${MBED_POST_BUILD_CYPRESS_DIR}/PSOC6.py
merge
--elf ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.elf
--m4hex ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.hex
--m0hex ${cortex_m0_hex}
)
else()
set(post_build_command
COMMAND ${Python3_EXECUTABLE} ${MBED_POST_BUILD_CYPRESS_DIR}/PSOC6.py
merge
--elf ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.elf
--m4hex ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.hex
)
add_custom_command(
TARGET
${target}
POST_BUILD
COMMAND
${post_build_command}
)
endfunction()
endif()

mbed_set_post_build_operation()
endfunction()
endmacro()


#
# Sign a Cortex-M4 HEX with Cortex-M0 HEX.
#
function(mbed_post_build_psoc6_sign_image
macro(mbed_post_build_psoc6_sign_image
m0hex_filename
mbed_target_name
cypress_psoc6_target
policy_file_name
boot_scheme
cm0_img_id
cm4_img_id
cortex_m0_hex
)
find_package(Python3)
if("${cypress_psoc6_target}" STREQUAL "${MBED_TARGET}")
function(mbed_post_build_function target)
find_package(Python3)

set(post_build_command
COMMAND ${Python3_EXECUTABLE} ${MBED_POST_BUILD_CYPRESS_DIR}/PSOC6.py
sign
--build-dir ${CMAKE_BINARY_DIR}
--m0hex-filename ${m0hex_filename}
--target-name ${mbed_target_name}
--policy-file-name ${policy_file_name}
--boot-scheme ${boot_scheme}
--cm0-img-id ${cm0_img_id}
--cm4-img-id ${cm4_img_id}
--elf ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.elf
--m4hex ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.hex
--m0hex ${cortex_m0_hex}
)
set(post_build_command
${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/PSOC6.py
sign
--build-dir ${CMAKE_BINARY_DIR}
--m0hex-filename ${m0hex_filename}
--target-name ${cypress_psoc6_target}
--policy-file-name ${policy_file_name}
--boot-scheme ${boot_scheme}
--cm0-img-id ${cm0_img_id}
--cm4-img-id ${cm4_img_id}
--elf $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.elf
--m4hex $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.hex
--m0hex ${cortex_m0_hex}
)

mbed_set_post_build_operation()
endfunction()
add_custom_command(
TARGET
${target}
POST_BUILD
COMMAND
${post_build_command}
)
endfunction()
endif()
endmacro()
35 changes: 20 additions & 15 deletions targets/TARGET_NUVOTON/scripts/mbed_set_post_build_nuvoton.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,27 @@ include(${MBED_PATH}/tools/cmake/mbed_set_post_build.cmake)
#
# Sign TF-M secure and non-secure images and combine them with the bootloader
#
function(mbed_post_build_nuvoton_tfm_sign_image_tgt
mbed_target
macro(mbed_post_build_nuvoton_tfm_sign_image_tgt
nuvoton_target
tfm_import_path
signing_key
)
find_package(Python3)
if("${nuvoton_target}" STREQUAL "${MBED_TARGET}")
function(mbed_post_build_function target)
find_package(Python3)

set(mbed_target_name ${mbed_target})
set(post_build_command
COMMAND ${Python3_EXECUTABLE}
${MBED_PATH}/targets/TARGET_NUVOTON/scripts/NUVOTON.py
tfm_sign_image_tgt
--tfm-import-path ${tfm_import_path}
--signing_key ${signing_key}
--non-secure-bin ${CMAKE_BINARY_DIR}/$<TARGET_PROPERTY:mbed-post-build-bin-${mbed_target_name},application>.bin
)

mbed_set_post_build_operation()
endfunction()
add_custom_command(
TARGET
${target}
POST_BUILD
COMMAND
${Python3_EXECUTABLE}
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/NUVOTON.py
tfm_sign_image_tgt
--tfm-import-path ${tfm_import_path}
--signing_key ${signing_key}
--non-secure-bin $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.bin
)
endfunction()
endif()
endmacro()
Loading