Skip to content

Commit 45a3c71

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 45a3c71

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

CMakeLists.txt

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,34 @@ 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+
# The first condition is quoted in case MBED_OUTPUT_EXT is unset
149+
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
150+
list(APPEND CMAKE_POST_BUILD_COMMAND
151+
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin
152+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
153+
)
154+
endif()
155+
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
156+
list(APPEND CMAKE_POST_BUILD_COMMAND
157+
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex
158+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
159+
)
160+
endif()
154161
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
155162
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"
163+
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
164+
list(APPEND CMAKE_POST_BUILD_COMMAND
165+
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin $<TARGET_FILE:${target}>
166+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
167+
)
168+
endif()
169+
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
170+
list(APPEND CMAKE_POST_BUILD_COMMAND
159171
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --i32combined -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex $<TARGET_FILE:${target}>
160172
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
161-
)
173+
)
174+
endif()
162175
endif()
163176
add_custom_command(
164177
TARGET

0 commit comments

Comments
 (0)