Skip to content

Commit 91add83

Browse files
committed
feat(cmakev2/size): add idf_create_size_report function
The idf_create_size_report function allows for the creation of size report targets based on the generated link map file. The size report targets are created using the TARGET option name: "<target>", "<target>-files", and "<target>-components". These size report targets are added to the idf_default_project with the TARGET set to "size", resulting in the creation of "size", "size-files", and "size-components" targets for the default project. Signed-off-by: Frantisek Hrbata <[email protected]>
1 parent 3318bd4 commit 91add83

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

tools/cmakev2/idf.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ include(compat)
3636
include(ldgen)
3737
include(dfu)
3838
include(uf2)
39+
include(size)
3940
include(GetGitRevisionDescription)
4041
# For backward compatibility, since externalproject_add is used by
4142
# project_include.cmake in the bootloader component. The ExternalProject

tools/cmakev2/project.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,11 @@ function(__project_default)
765765
idf_create_uf2("${executable}"
766766
TARGET uf2-app
767767
APP_ONLY)
768+
769+
if(TARGET "${executable}_mapfile")
770+
idf_create_size_report("${executable}_mapfile"
771+
TARGET size)
772+
endif()
768773
endfunction()
769774

770775
#[[api

tools/cmakev2/size.cmake

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#[[
5+
.. cmakev2:function:: idf_create_size_report
6+
7+
.. code-block:: cmake
8+
9+
idf_create_size_report(<mapfile>
10+
TARGET <target>)
11+
12+
*mapfile[in]*
13+
14+
The mapfile target generated by the idf_build_executable function.
15+
16+
*TARGET[in]*
17+
18+
The base name for the size report targets to be created. In addition to
19+
the default size report, which will be available under the target name
20+
specified in the ``TARGET`` option, two more detailed targets,
21+
"<target>-files" and "<target>-components", will also be created.
22+
23+
Create size report targets for the specified ``mapfile`` target. The
24+
``TARGET`` option specifies the name of the default size report target, but
25+
two more targets with detailed reports are also created. For example, if
26+
``TARGET`` is set to "size," three targets "size", "size-files", and
27+
"size-components" will be created.
28+
#]]
29+
function(idf_create_size_report mapfile_target)
30+
set(options)
31+
set(one_value TARGET)
32+
set(multi_value)
33+
cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN})
34+
35+
if(NOT DEFINED ARG_TARGET)
36+
idf_die("TARGET option is required")
37+
endif()
38+
39+
get_target_property(mapfile "${mapfile_target}" MAPFILE_PATH)
40+
if(NOT mapfile)
41+
idf_die("Mapfile target '${mapfile_target}' is missing 'MAPFILE_PATH' property.")
42+
endif()
43+
44+
idf_build_get_property(idf_path IDF_PATH)
45+
idf_build_get_property(python PYTHON)
46+
47+
set(idf_size ${python} -m esp_idf_size)
48+
49+
add_custom_target(${ARG_TARGET}
50+
COMMAND ${CMAKE_COMMAND}
51+
-D "IDF_SIZE_TOOL=${idf_size}"
52+
-D "MAP_FILE=${mapfile}"
53+
-D "OUTPUT_JSON=${OUTPUT_JSON}"
54+
-P "${idf_path}/tools/cmake/run_size_tool.cmake"
55+
DEPENDS ${mapfile_target}
56+
USES_TERMINAL
57+
VERBATIM
58+
)
59+
60+
add_custom_target(${ARG_TARGET}-files
61+
COMMAND ${CMAKE_COMMAND}
62+
-D "IDF_SIZE_TOOL=${idf_size}"
63+
-D "IDF_SIZE_MODE=--files"
64+
-D "MAP_FILE=${mapfile}"
65+
-D "OUTPUT_JSON=${OUTPUT_JSON}"
66+
-P "${idf_path}/tools/cmake/run_size_tool.cmake"
67+
DEPENDS ${mapfile_target}
68+
USES_TERMINAL
69+
VERBATIM
70+
)
71+
72+
add_custom_target(${ARG_TARGET}-components
73+
COMMAND ${CMAKE_COMMAND}
74+
-D "IDF_SIZE_TOOL=${idf_size}"
75+
-D "IDF_SIZE_MODE=--archives"
76+
-D "MAP_FILE=${mapfile}"
77+
-D "OUTPUT_JSON=${OUTPUT_JSON}"
78+
-P "${idf_path}/tools/cmake/run_size_tool.cmake"
79+
DEPENDS ${mapfile_target}
80+
USES_TERMINAL
81+
VERBATIM
82+
)
83+
endfunction()

tools/cmakev2/test/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ function(test_executable)
235235
idf_create_confserver(fatfs_example
236236
TARGET confserver-fatfs)
237237

238+
if(TARGET fatfs_example_mapfile)
239+
idf_create_size_report(fatfs_example_mapfile
240+
TARGET fatfs-size)
241+
endif()
242+
238243

239244
idf_build_executable(hello_world_example
240245
COMPONENTS hello_world_example
@@ -252,6 +257,11 @@ function(test_executable)
252257
OUTPUT_FILE project_description_hello_world.json)
253258
idf_create_confserver(hello_world_example
254259
TARGET confserver-hello_world)
260+
261+
if(TARGET hello_world_example_mapfile)
262+
idf_create_size_report(hello_world_example_mapfile
263+
TARGET hello-size)
264+
endif()
255265
endfunction()
256266

257267
# Run tests

0 commit comments

Comments
 (0)