Skip to content

Commit c56f0c8

Browse files
committed
CMake: Image format based on MBED_OUTPUT_EXT
Requires: ARMmbed/mbed-tools#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.
1 parent 54a5fc5 commit c56f0c8

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

CMakeLists.txt

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,33 @@ target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
144144
#
145145
function(mbed_generate_bin_hex target)
146146
get_property(elf_to_bin GLOBAL PROPERTY ELF2BIN)
147-
if(MBED_TOOLCHAIN STREQUAL "GCC_ARM")
148-
set(CMAKE_POST_BUILD_COMMAND
149-
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin
150-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
151-
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex
152-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
153-
)
147+
if (MBED_TOOLCHAIN STREQUAL "GCC_ARM")
148+
if (MBED_OUTPUT_EXT STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
149+
list(APPEND CMAKE_POST_BUILD_COMMAND
150+
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin
151+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
152+
)
153+
endif()
154+
if (MBED_OUTPUT_EXT STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
155+
list(APPEND CMAKE_POST_BUILD_COMMAND
156+
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex
157+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
158+
)
159+
endif()
154160
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
155161
get_property(mbed_studio_arm_compiler GLOBAL PROPERTY MBED_STUDIO_ARM_COMPILER)
156-
set(CMAKE_POST_BUILD_COMMAND
157-
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin $<TARGET_FILE:${target}>
158-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
162+
if (MBED_OUTPUT_EXT STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
163+
list(APPEND CMAKE_POST_BUILD_COMMAND
164+
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin $<TARGET_FILE:${target}>
165+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
166+
)
167+
endif()
168+
if (MBED_OUTPUT_EXT STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
169+
list(APPEND CMAKE_POST_BUILD_COMMAND
159170
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --i32combined -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex $<TARGET_FILE:${target}>
160171
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
161-
)
172+
)
173+
endif()
162174
endif()
163175
add_custom_command(
164176
TARGET

0 commit comments

Comments
 (0)