diff --git a/tools/cmakev2/idf.cmake b/tools/cmakev2/idf.cmake index 50b4368e6a..6824a9296c 100644 --- a/tools/cmakev2/idf.cmake +++ b/tools/cmakev2/idf.cmake @@ -36,6 +36,7 @@ include(compat) include(ldgen) include(dfu) include(uf2) +include(size) include(GetGitRevisionDescription) # For backward compatibility, since externalproject_add is used by # project_include.cmake in the bootloader component. The ExternalProject diff --git a/tools/cmakev2/project.cmake b/tools/cmakev2/project.cmake index dcc29f6497..f42712b516 100644 --- a/tools/cmakev2/project.cmake +++ b/tools/cmakev2/project.cmake @@ -765,6 +765,11 @@ function(__project_default) idf_create_uf2("${executable}" TARGET uf2-app APP_ONLY) + + if(TARGET "${executable}_mapfile") + idf_create_size_report("${executable}_mapfile" + TARGET size) + endif() endfunction() #[[api diff --git a/tools/cmakev2/size.cmake b/tools/cmakev2/size.cmake new file mode 100644 index 0000000000..17deff59c3 --- /dev/null +++ b/tools/cmakev2/size.cmake @@ -0,0 +1,83 @@ +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 + +#[[ +.. cmakev2:function:: idf_create_size_report + + .. code-block:: cmake + + idf_create_size_report( + TARGET ) + + *mapfile[in]* + + The mapfile target generated by the idf_build_executable function. + + *TARGET[in]* + + The base name for the size report targets to be created. In addition to + the default size report, which will be available under the target name + specified in the ``TARGET`` option, two more detailed targets, + "-files" and "-components", will also be created. + + Create size report targets for the specified ``mapfile`` target. The + ``TARGET`` option specifies the name of the default size report target, but + two more targets with detailed reports are also created. For example, if + ``TARGET`` is set to "size," three targets "size", "size-files", and + "size-components" will be created. +#]] +function(idf_create_size_report mapfile_target) + set(options) + set(one_value TARGET) + set(multi_value) + cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN}) + + if(NOT DEFINED ARG_TARGET) + idf_die("TARGET option is required") + endif() + + get_target_property(mapfile "${mapfile_target}" MAPFILE_PATH) + if(NOT mapfile) + idf_die("Mapfile target '${mapfile_target}' is missing 'MAPFILE_PATH' property.") + endif() + + idf_build_get_property(idf_path IDF_PATH) + idf_build_get_property(python PYTHON) + + set(idf_size ${python} -m esp_idf_size) + + add_custom_target(${ARG_TARGET} + COMMAND ${CMAKE_COMMAND} + -D "IDF_SIZE_TOOL=${idf_size}" + -D "MAP_FILE=${mapfile}" + -D "OUTPUT_JSON=${OUTPUT_JSON}" + -P "${idf_path}/tools/cmake/run_size_tool.cmake" + DEPENDS ${mapfile_target} + USES_TERMINAL + VERBATIM + ) + + add_custom_target(${ARG_TARGET}-files + COMMAND ${CMAKE_COMMAND} + -D "IDF_SIZE_TOOL=${idf_size}" + -D "IDF_SIZE_MODE=--files" + -D "MAP_FILE=${mapfile}" + -D "OUTPUT_JSON=${OUTPUT_JSON}" + -P "${idf_path}/tools/cmake/run_size_tool.cmake" + DEPENDS ${mapfile_target} + USES_TERMINAL + VERBATIM + ) + + add_custom_target(${ARG_TARGET}-components + COMMAND ${CMAKE_COMMAND} + -D "IDF_SIZE_TOOL=${idf_size}" + -D "IDF_SIZE_MODE=--archives" + -D "MAP_FILE=${mapfile}" + -D "OUTPUT_JSON=${OUTPUT_JSON}" + -P "${idf_path}/tools/cmake/run_size_tool.cmake" + DEPENDS ${mapfile_target} + USES_TERMINAL + VERBATIM + ) +endfunction() diff --git a/tools/cmakev2/test/CMakeLists.txt b/tools/cmakev2/test/CMakeLists.txt index 74608a7ad9..bc0e3fa2e5 100644 --- a/tools/cmakev2/test/CMakeLists.txt +++ b/tools/cmakev2/test/CMakeLists.txt @@ -235,6 +235,11 @@ function(test_executable) idf_create_confserver(fatfs_example TARGET confserver-fatfs) + if(TARGET fatfs_example_mapfile) + idf_create_size_report(fatfs_example_mapfile + TARGET fatfs-size) + endif() + idf_build_executable(hello_world_example COMPONENTS hello_world_example @@ -252,6 +257,11 @@ function(test_executable) OUTPUT_FILE project_description_hello_world.json) idf_create_confserver(hello_world_example TARGET confserver-hello_world) + + if(TARGET hello_world_example_mapfile) + idf_create_size_report(hello_world_example_mapfile + TARGET hello-size) + endif() endfunction() # Run tests