From 45a3c7193f0f251b9170fe27c3c6be6af7e5f6a7 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Thu, 25 Mar 2021 12:40:33 +0000 Subject: [PATCH] CMake: Image format based on MBED_OUTPUT_EXT Requires: https://github.com/ARMmbed/mbed-tools/pull/242 The optional `OUTPUT_EXT` field in Mbed OS `targets.json` specifies the format of the output image. This is important because * some targets only support one image format * each post-binary hook outputs one image only This avoids confusion of having both .hex and .bin images generated while only one of them is usable. --- CMakeLists.txt | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ad61efd96b..03ac543a45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -144,21 +144,34 @@ target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED}) # function(mbed_generate_bin_hex target) get_property(elf_to_bin GLOBAL PROPERTY ELF2BIN) - if(MBED_TOOLCHAIN STREQUAL "GCC_ARM") - set(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" - COMMAND ${elf_to_bin} -O ihex $ ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex - COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex" - ) + 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) - set(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" + 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