diff --git a/CMakeLists.txt b/CMakeLists.txt index 186825ad08..e961e388a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,122 +171,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 $ ${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 $ ${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 $ - 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 $ - 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 diff --git a/tools/cmake/app.cmake b/tools/cmake/app.cmake index 500e477a4c..60f9f855ff 100644 --- a/tools/cmake/app.cmake +++ b/tools/cmake/app.cmake @@ -11,6 +11,7 @@ if(CCACHE) endif() include(${MBED_CONFIG_PATH}/mbed_config.cmake) +include(mbed_set_post_build) # Load toolchain file if(NOT CMAKE_TOOLCHAIN_FILE OR MBED_TOOLCHAIN_FILE_USED) diff --git a/tools/cmake/mbed_set_post_build.cmake b/tools/cmake/mbed_set_post_build.cmake index 22363e7f52..f42d94317b 100644 --- a/tools/cmake/mbed_set_post_build.cmake +++ b/tools/cmake/mbed_set_post_build.cmake @@ -1,6 +1,123 @@ # Copyright (c) 2021 ARM Limited. All rights reserved. # SPDX-License-Identifier: Apache-2.0 +# +# 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 $ ${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 $ ${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 $ + 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 $ + 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() + + # # Sets the post build operation for Mbed targets. #