Skip to content

Commit fcd57b2

Browse files
rwalton-armLDong-Arm
authored andcommitted
CMake: Move post build functions out of root CMakeLists.txt
The 'post build' functions are made visible by adding the mbed-os subdirectory. This is not ideal as any components in mbed-os wishing to call the functions must be added after the functions are defined. To improve modularity move these functions to a separate CMake script. We include the post build CMake script in app.cmake for now so we don't break user's projects.
1 parent 662bd59 commit fcd57b2

File tree

3 files changed

+118
-116
lines changed

3 files changed

+118
-116
lines changed

CMakeLists.txt

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -171,122 +171,6 @@ if(${CMAKE_CROSSCOMPILING})
171171
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET_CONVERTED})
172172
endif()
173173

174-
#
175-
# Converts output file of `target` to binary file and to Intel HEX file.
176-
#
177-
function(mbed_generate_bin_hex target)
178-
get_property(elf_to_bin GLOBAL PROPERTY ELF2BIN)
179-
if (MBED_TOOLCHAIN STREQUAL "GCC_ARM")
180-
# The first condition is quoted in case MBED_OUTPUT_EXT is unset
181-
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
182-
list(APPEND CMAKE_POST_BUILD_COMMAND
183-
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin
184-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
185-
)
186-
endif()
187-
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
188-
list(APPEND CMAKE_POST_BUILD_COMMAND
189-
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex
190-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
191-
)
192-
endif()
193-
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
194-
get_property(mbed_studio_arm_compiler GLOBAL PROPERTY MBED_STUDIO_ARM_COMPILER)
195-
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
196-
list(APPEND CMAKE_POST_BUILD_COMMAND
197-
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin $<TARGET_FILE:${target}>
198-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
199-
)
200-
endif()
201-
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
202-
list(APPEND CMAKE_POST_BUILD_COMMAND
203-
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --i32combined -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex $<TARGET_FILE:${target}>
204-
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
205-
)
206-
endif()
207-
endif()
208-
add_custom_command(
209-
TARGET
210-
${target}
211-
POST_BUILD
212-
${CMAKE_POST_BUILD_COMMAND}
213-
COMMENT
214-
"executable:"
215-
VERBATIM
216-
)
217-
218-
if(TARGET mbed-post-build-bin-${MBED_TARGET})
219-
# Remove the .elf file to force regenerate the application binaries
220-
# (including .bin and .hex). This ensures that the post-build script runs
221-
# on a raw application instead of a previous build that already went
222-
# through the post-build process once.
223-
file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/${target}.elf)
224-
225-
# Pass the application's name to the Mbed target's post build operation
226-
set_target_properties(mbed-post-build-bin-${MBED_TARGET}
227-
PROPERTIES
228-
application ${target}
229-
)
230-
231-
# The artefacts must be created before they can be further manipulated
232-
add_dependencies(mbed-post-build-bin-${MBED_TARGET} ${target})
233-
234-
# Add a post-build hook to the top-level CMake target in the form of a
235-
# CMake custom target. The hook depends on Mbed target specific
236-
# post-build CMake target which has a custom command attached to it.
237-
add_custom_target(mbed-post-build ALL DEPENDS mbed-post-build-bin-${MBED_TARGET})
238-
endif()
239-
endfunction()
240-
241-
#
242-
# Parse toolchain generated map file of `target` and display a readable table format.
243-
#
244-
function(mbed_generate_map_file target)
245-
add_custom_command(
246-
TARGET
247-
${target}
248-
POST_BUILD
249-
COMMAND ${Python3_EXECUTABLE} ${mbed-os_SOURCE_DIR}/tools/memap.py -t ${MBED_TOOLCHAIN} ${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map
250-
WORKING_DIRECTORY
251-
${CMAKE_CURRENT_BINARY_DIR}
252-
COMMENT
253-
"Displaying memory map for ${target}"
254-
)
255-
endfunction()
256-
257-
#
258-
# Validate selected application profile.
259-
#
260-
function(mbed_validate_application_profile target)
261-
get_target_property(app_link_libraries ${target} LINK_LIBRARIES)
262-
string(FIND "${app_link_libraries}" "mbed-baremetal" string_found_position)
263-
if(${string_found_position} GREATER_EQUAL 0)
264-
if(NOT "bare-metal" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
265-
message(FATAL_ERROR
266-
"Use full profile as baremetal profile is not supported for this Mbed target")
267-
endif()
268-
elseif(NOT "full" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
269-
message(FATAL_ERROR
270-
"The full profile is not supported for this Mbed target")
271-
endif()
272-
endfunction()
273-
274-
#
275-
# Set post build operations
276-
#
277-
function(mbed_set_post_build target)
278-
# The mapfile name includes the top-level target name and the
279-
# executable suffix for all toolchains as CMake hardcodes the name of the
280-
# diagnostic output file for some toolchains.
281-
mbed_configure_memory_map(${target} "${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map")
282-
mbed_validate_application_profile(${target})
283-
mbed_generate_bin_hex(${target})
284-
285-
if(HAVE_MEMAP_DEPS)
286-
mbed_generate_map_file(${target})
287-
endif()
288-
endfunction()
289-
290174
# Ninja requires to be forced for response files
291175
if ("${CMAKE_GENERATOR}" MATCHES "Ninja")
292176
# known issue ARMClang and Ninja with response files for windows

tools/cmake/app.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if(CCACHE)
1111
endif()
1212

1313
include(${MBED_CONFIG_PATH}/mbed_config.cmake)
14+
include(mbed_set_post_build)
1415

1516
# Load toolchain file
1617
if(NOT CMAKE_TOOLCHAIN_FILE OR MBED_TOOLCHAIN_FILE_USED)

tools/cmake/mbed_set_post_build.cmake

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,123 @@
11
# Copyright (c) 2021 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4+
#
5+
# Converts output file of `target` to binary file and to Intel HEX file.
6+
#
7+
function(mbed_generate_bin_hex target)
8+
get_property(elf_to_bin GLOBAL PROPERTY ELF2BIN)
9+
if (MBED_TOOLCHAIN STREQUAL "GCC_ARM")
10+
# The first condition is quoted in case MBED_OUTPUT_EXT is unset
11+
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
12+
list(APPEND CMAKE_POST_BUILD_COMMAND
13+
COMMAND ${elf_to_bin} -O binary $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin
14+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
15+
)
16+
endif()
17+
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
18+
list(APPEND CMAKE_POST_BUILD_COMMAND
19+
COMMAND ${elf_to_bin} -O ihex $<TARGET_FILE:${target}> ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex
20+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
21+
)
22+
endif()
23+
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
24+
get_property(mbed_studio_arm_compiler GLOBAL PROPERTY MBED_STUDIO_ARM_COMPILER)
25+
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "bin")
26+
list(APPEND CMAKE_POST_BUILD_COMMAND
27+
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --bin -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin $<TARGET_FILE:${target}>
28+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.bin"
29+
)
30+
endif()
31+
if ("${MBED_OUTPUT_EXT}" STREQUAL "" OR MBED_OUTPUT_EXT STREQUAL "hex")
32+
list(APPEND CMAKE_POST_BUILD_COMMAND
33+
COMMAND ${elf_to_bin} ${mbed_studio_arm_compiler} --i32combined -o ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex $<TARGET_FILE:${target}>
34+
COMMAND ${CMAKE_COMMAND} -E echo "-- built: ${CMAKE_CURRENT_BINARY_DIR}/${target}.hex"
35+
)
36+
endif()
37+
endif()
38+
add_custom_command(
39+
TARGET
40+
${target}
41+
POST_BUILD
42+
${CMAKE_POST_BUILD_COMMAND}
43+
COMMENT
44+
"executable:"
45+
VERBATIM
46+
)
47+
48+
if(TARGET mbed-post-build-bin-${MBED_TARGET})
49+
# Remove the .elf file to force regenerate the application binaries
50+
# (including .bin and .hex). This ensures that the post-build script runs
51+
# on a raw application instead of a previous build that already went
52+
# through the post-build process once.
53+
file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/${target}.elf)
54+
55+
# Pass the application's name to the Mbed target's post build operation
56+
set_target_properties(mbed-post-build-bin-${MBED_TARGET}
57+
PROPERTIES
58+
application ${target}
59+
)
60+
61+
# The artefacts must be created before they can be further manipulated
62+
add_dependencies(mbed-post-build-bin-${MBED_TARGET} ${target})
63+
64+
# Add a post-build hook to the top-level CMake target in the form of a
65+
# CMake custom target. The hook depends on Mbed target specific
66+
# post-build CMake target which has a custom command attached to it.
67+
add_custom_target(mbed-post-build ALL DEPENDS mbed-post-build-bin-${MBED_TARGET})
68+
endif()
69+
endfunction()
70+
71+
#
72+
# Parse toolchain generated map file of `target` and display a readable table format.
73+
#
74+
function(mbed_generate_map_file target)
75+
add_custom_command(
76+
TARGET
77+
${target}
78+
POST_BUILD
79+
COMMAND ${Python3_EXECUTABLE} ${mbed-os_SOURCE_DIR}/tools/memap.py -t ${MBED_TOOLCHAIN} ${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map
80+
WORKING_DIRECTORY
81+
${CMAKE_CURRENT_BINARY_DIR}
82+
COMMENT
83+
"Displaying memory map for ${target}"
84+
)
85+
endfunction()
86+
87+
#
88+
# Validate selected application profile.
89+
#
90+
function(mbed_validate_application_profile target)
91+
get_target_property(app_link_libraries ${target} LINK_LIBRARIES)
92+
string(FIND "${app_link_libraries}" "mbed-baremetal" string_found_position)
93+
if(${string_found_position} GREATER_EQUAL 0)
94+
if(NOT "bare-metal" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
95+
message(FATAL_ERROR
96+
"Use full profile as baremetal profile is not supported for this Mbed target")
97+
endif()
98+
elseif(NOT "full" IN_LIST MBED_TARGET_SUPPORTED_APPLICATION_PROFILES)
99+
message(FATAL_ERROR
100+
"The full profile is not supported for this Mbed target")
101+
endif()
102+
endfunction()
103+
104+
#
105+
# Set post build operations
106+
#
107+
function(mbed_set_post_build target)
108+
# The mapfile name includes the top-level target name and the
109+
# executable suffix for all toolchains as CMake hardcodes the name of the
110+
# diagnostic output file for some toolchains.
111+
mbed_configure_memory_map(${target} "${CMAKE_CURRENT_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map")
112+
mbed_validate_application_profile(${target})
113+
mbed_generate_bin_hex(${target})
114+
115+
if(HAVE_MEMAP_DEPS)
116+
mbed_generate_map_file(${target})
117+
endif()
118+
endfunction()
119+
120+
4121
#
5122
# Sets the post build operation for Mbed targets.
6123
#

0 commit comments

Comments
 (0)