Skip to content

Commit 0825f0f

Browse files
committed
Merge branch 'feat/cmakev2_size_targets_v6.0' into 'release/v6.0'
fix(cmakev2): add size report targets (v6.0) See merge request espressif/esp-idf!43546
2 parents ad29ffc + 91add83 commit 0825f0f

File tree

6 files changed

+250
-84
lines changed

6 files changed

+250
-84
lines changed

tools/cmakev2/build.cmake

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ function(idf_build_set_property property value)
3737
set(multi_value)
3838
cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN})
3939

40+
if("${property}" STREQUAL MINIMAL_BUILD)
41+
idf_warn("Build property 'MINIMAL_BUILD' is obsolete and will be ignored")
42+
endif()
43+
4044
set(append)
4145
if(ARG_APPEND)
4246
set(append APPEND)
@@ -503,15 +507,27 @@ endfunction()
503507
504508
Optional ``executable`` suffix.
505509
510+
*MAPFILE_TARGET[in,opt]*
511+
512+
Name of the target for the map file. If provided, the link map file is
513+
generated for the specified executable, and the ``MAPFILE_TARGET``
514+
target name is created for it. The ``MAPFILE_PATH`` property with the
515+
link map file path is added to the ``MAPFILE_TARGET`` target. This can
516+
be used for other targets that depend on the link map file. The link map file
517+
is not generated on Darwin host, so the target ``MAPFILE_TARGET`` may not
518+
be created if link map file is not generated.
519+
506520
Create a new executable target using the name specified in the
507521
``executable`` argument, and link it to the library created with the
508522
component names provided in the ``COMPONENTS`` option. If the
509523
``COMPONENTS`` option is not set, all discovered components are added to
510-
the library. Optinaly set the executable name and suffix.
524+
the library. Optionally set the executable name and suffix. The executable
525+
library target name is added to the ``LIBRARY_INTERFACE`` executable
526+
property.
511527
#]]
512528
function(idf_build_executable executable)
513529
set(options)
514-
set(one_value NAME SUFFIX)
530+
set(one_value NAME SUFFIX MAPFILE_TARGET)
515531
set(multi_value COMPONENTS)
516532
cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN})
517533

@@ -534,15 +550,29 @@ function(idf_build_executable executable)
534550
endif()
535551
add_executable(${executable} "${executable_src}")
536552

537-
if(ARG_NAME)
538-
set_target_properties(${executable} PROPERTIES OUTPUT_NAME ${ARG_NAME})
539-
endif()
553+
set_target_properties(${executable} PROPERTIES OUTPUT_NAME ${ARG_NAME})
540554

541555
if(ARG_SUFFIX)
542556
set_target_properties(${executable} PROPERTIES SUFFIX ${ARG_SUFFIX})
543557
endif()
544558

545559
target_link_libraries(${executable} PRIVATE ${library})
560+
561+
idf_build_get_property(linker_type LINKER_TYPE)
562+
if(ARG_MAPFILE_TARGET AND "${linker_type}" STREQUAL "GNU")
563+
set(mapfile "${CMAKE_BINARY_DIR}/${ARG_NAME}.map")
564+
target_link_options(${executable} PRIVATE "LINKER:--Map=${mapfile}")
565+
add_custom_command(
566+
OUTPUT "${mapfile}"
567+
DEPENDS ${executable}
568+
)
569+
add_custom_target(${ARG_MAPFILE_TARGET}
570+
DEPENDS "${mapfile}"
571+
)
572+
set_target_properties(${ARG_MAPFILE_TARGET} PROPERTIES MAPFILE_PATH ${mapfile})
573+
endif()
574+
575+
set_target_properties(${executable} PROPERTIES LIBRARY_INTERFACE ${library})
546576
endfunction()
547577

548578
#[[
@@ -647,28 +677,34 @@ endfunction()
647677
648678
.. code-block:: cmake
649679
650-
idf_build_generate_metadata(<executable>
680+
idf_build_generate_metadata(<binary>
651681
[FILE <file>])
652682
653-
*executable[in]*
683+
*binary[in]*
654684
655-
Executable target for which to generate a metadata file.
685+
Binary target for which to generate a metadata file.
656686
657687
*OUTPUT_FILE[in,opt]*
658688
659689
Optional output file path for storing the metadata. If not provided,
660690
the default path ``<build>/project_description.json`` is used.
661691
662-
Generate metadata for the specified ``executable`` and store it in the
692+
Generate metadata for the specified ``binary`` and store it in the
663693
specified ``FILE``. If no ``FILE`` is provided, the default location
664694
``<build>/project_description.json`` will be used.
665695
#]]
666-
function(idf_build_generate_metadata executable)
696+
function(idf_build_generate_metadata binary)
667697
set(options)
668698
set(one_value OUTPUT_FILE)
669699
set(multi_value)
670700
cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN})
671701

702+
# The EXECUTABLE_TARGET property is set by the idf_build_binary or
703+
# the idf_sign_binary function.
704+
get_target_property(executable "${binary}" EXECUTABLE_TARGET)
705+
if(NOT executable)
706+
idf_die("Binary target '${binary}' is missing 'EXECUTABLE_TARGET' property.")
707+
endif()
672708
__get_executable_library_or_die(TARGET "${executable}" OUTPUT library)
673709

674710
idf_build_get_property(PROJECT_NAME PROJECT_NAME)
@@ -679,9 +715,13 @@ function(idf_build_generate_metadata executable)
679715
idf_build_get_property(SDKCONFIG SDKCONFIG)
680716
idf_build_get_property(SDKCONFIG_DEFAULTS SDKCONFIG_DEFAULTS)
681717
set(PROJECT_EXECUTABLE "$<TARGET_FILE_NAME:${executable}>")
682-
# The PROJECT_BIN executable property must be set by the idf_build_binary
683-
# function.
684-
get_target_property(PROJECT_BIN "${executable}" EXECUTABLE_BINARY)
718+
# The BINARY_PATH property is set by the idf_build_binary or
719+
# the idf_sign_binary function.
720+
get_target_property(binary_path ${binary} BINARY_PATH)
721+
if(NOT binary_path)
722+
idf_die("Binary target '${binary}' is missing 'BINARY_PATH' property.")
723+
endif()
724+
get_filename_component(PROJECT_BIN "${binary_path}" NAME)
685725
if(NOT PROJECT_BIN)
686726
set(PROJECT_BIN "")
687727
endif()
@@ -767,8 +807,9 @@ endfunction()
767807
Create a binary image for the specified ``executable`` target and save it
768808
in the file specified with the ``OUTPUT_FILE`` option. A custom target
769809
named ``TARGET`` will be created for the generated binary image. The path
770-
of the generated binary image will be also stored in the ``BINARY_PATH``
771-
property of the ``TARGET``.
810+
of the generated binary image will be stored in the ``BINARY_PATH``
811+
property and the executable target in the ``EXECUTABLE_TARGET`` property of
812+
the ``TARGET``.
772813
#]]
773814
function(idf_build_binary executable)
774815
set(options)
@@ -828,13 +869,13 @@ function(idf_build_binary executable)
828869
# Create a custom target to generate the binary file
829870
add_custom_target(${ARG_TARGET} DEPENDS "${ARG_OUTPUT_FILE}")
830871

831-
# The EXECUTABLE_BINARY property is used by idf_build_generate_metadata to
832-
# store the name of the binary image.
833-
set_target_properties(${executable} PROPERTIES EXECUTABLE_BINARY ${binary_name})
834-
835872
# Store the path of the binary file in the BINARY_PATH property of the
836873
# custom binary target, which is used by the idf_flash_binary.
837874
set_target_properties(${ARG_TARGET} PROPERTIES BINARY_PATH ${ARG_OUTPUT_FILE})
875+
876+
# Store executable target name in the EXECUTABLE_TARGET property. This is used
877+
# by the idf_build_generate_metadata function.
878+
set_target_properties(${ARG_TARGET} PROPERTIES EXECUTABLE_TARGET ${executable})
838879
endfunction()
839880

840881
#[[
@@ -870,9 +911,10 @@ endfunction()
870911
871912
Sign binary image specified by ``binary`` target with ``KEYFILE`` and save
872913
it in the file specified with the `OUTPUT_FILE` option. A custom target
873-
named ``TARGET`` will be created for the signed binary image. The path of
874-
the signed binary image will be also stored in the ``BINARY_PATH`` property
875-
of the ``TARGET``.
914+
named ``TARGET`` will be created for the signed binary image. The path of
915+
the signed binary image will be stored in the ``BINARY_PATH`` property and
916+
the executable target in the ``EXECUTABLE_TARGET`` property of the
917+
``TARGET``.
876918
#]]
877919
function(idf_sign_binary binary)
878920
set(options)
@@ -936,6 +978,14 @@ function(idf_sign_binary binary)
936978
# Store the path of the binary file in the BINARY_PATH property of the
937979
# custom signed binary target, which is used by the idf_flash_binary.
938980
set_target_properties(${ARG_TARGET} PROPERTIES BINARY_PATH ${ARG_OUTPUT_FILE})
981+
982+
# Store executable target name in the EXECUTABLE_TARGET property. This is used
983+
# by the idf_build_generate_metadata function.
984+
get_target_property(executable "${binary}" EXECUTABLE_TARGET)
985+
if(NOT executable)
986+
idf_die("Binary target '${binary}' is missing 'EXECUTABLE_TARGET' property.")
987+
endif()
988+
set_target_properties(${ARG_TARGET} PROPERTIES EXECUTABLE_TARGET ${executable})
939989
endfunction()
940990

941991
#[[

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: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ function(__init_project_configuration)
9090
idf_build_get_property(project_dir PROJECT_DIR)
9191
idf_build_get_property(project_name PROJECT_NAME)
9292

93+
# Set the LINKER_TYPE build property. Different linkers may have varying
94+
# options, so it's important to identify the linker type to configure the
95+
# options correctly. Currently, LINKER_TYPE is used to set the appropriate
96+
# linker options for linking the entire archive, which differs between the
97+
# GNU and Apple linkers when building on the host.
98+
if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
99+
# Compiling for the host, and the host is macOS, so the linker is Darwin LD.
100+
# Note, when adding support for Clang and LLD based toolchain this check will
101+
# need to be modified.
102+
set(linker_type "Darwin")
103+
else()
104+
set(linker_type "GNU")
105+
endif()
106+
idf_build_set_property(LINKER_TYPE "${linker_type}")
107+
93108
list(APPEND compile_definitions "_GLIBCXX_USE_POSIX_SEMAPHORE" # These two lines enable libstd++ to use
94109
"_GLIBCXX_HAVE_POSIX_SEMAPHORE" # posix-semaphores from components/pthread
95110
"_GNU_SOURCE")
@@ -407,16 +422,13 @@ function(__init_project_configuration)
407422
list(APPEND link_options "-specs=picolibc.specs")
408423
endif()
409424

410-
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
411-
set(mapfile "${build_dir}/${project_name}.map")
425+
if("${linker_type}" STREQUAL "GNU")
412426
set(target_upper "${idf_target}")
413427
string(TOUPPER ${target_upper} target_upper)
414428
# Add cross-reference table to the map file
415429
list(APPEND link_options "-Wl,--cref")
416430
# Add this symbol as a hint for esp_idf_size to guess the target name
417431
list(APPEND link_options "-Wl,--defsym=IDF_TARGET_${target_upper}=0")
418-
# Enable map file output
419-
list(APPEND link_options "-Wl,--Map=${mapfile}")
420432
# Check if linker supports --no-warn-rwx-segments
421433
execute_process(COMMAND ${CMAKE_LINKER} "--no-warn-rwx-segments" "--version"
422434
RESULT_VARIABLE result
@@ -486,21 +498,6 @@ function(__init_project_configuration)
486498
idf_build_set_property(ASM_COMPILE_OPTIONS "${asm_compile_options}" APPEND)
487499
idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND)
488500
idf_build_set_property(LINK_OPTIONS "${link_options}" APPEND)
489-
490-
# Set the LINKER_TYPE build property. Different linkers may have varying
491-
# options, so it's important to identify the linker type to configure the
492-
# options correctly. Currently, LINKER_TYPE is used to set the appropriate
493-
# linker options for linking the entire archive, which differs between the
494-
# GNU and Apple linkers when building on the host.
495-
if(CONFIG_IDF_TARGET_LINUX AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
496-
# Compiling for the host, and the host is macOS, so the linker is Darwin LD.
497-
# Note, when adding support for Clang and LLD based toolchain this check will
498-
# need to be modified.
499-
set(linker_type "Darwin")
500-
else()
501-
set(linker_type "GNU")
502-
endif()
503-
idf_build_set_property(LINKER_TYPE "${linker_type}")
504501
endfunction()
505502

506503
#[[
@@ -599,6 +596,7 @@ macro(idf_project_init)
599596
idf_die("sdkconfig.cmake file not found.")
600597
endif()
601598
include("${sdkconfig_cmake}")
599+
unset(sdkconfig_cmake)
602600

603601
# Initialize the target architecture based on the configuration
604602
# Ensure this is done after including the sdkconfig.
@@ -639,6 +637,7 @@ macro(idf_project_init)
639637
idf_component_include("${component_name}")
640638
endforeach()
641639
endif()
640+
unset(include_all_components)
642641

643642
idf_build_set_property(__PROJECT_INITIALIZED YES)
644643
endif()
@@ -694,26 +693,22 @@ function(idf_build_generate_flasher_args)
694693
INPUT "${build_dir}/flasher_args.json.in")
695694
endfunction()
696695

697-
#[[api
698-
.. cmakev2:macro:: idf_project_default
696+
#[[
697+
.. cmakev2:macro:: __project_default
699698
700699
.. code-block:: cmake
701700
702-
idf_project_default()
701+
__project_default()
703702
704-
Create a default project executable based on the main component and its
705-
transitive dependencies. The executable name is derived from the
706-
``PROJECT_NAME`` variable, which by default uses the ``CMAKE_PROJECT_NAME``
707-
value specified in the CMake's ``project()`` call.
708-
709-
Generate the binary image for the executable, signed or unsigned based on
710-
the configuration, and add flash targets for it.
703+
Helper function implementing the main idf_project_default macro
704+
functionality, preventing global variable scope pollution.
711705
#]]
712-
macro(idf_project_default)
713-
idf_project_init()
706+
function(__project_default)
714707
idf_build_get_property(build_dir BUILD_DIR)
715708
idf_build_get_property(executable PROJECT_NAME)
716-
idf_build_executable("${executable}" COMPONENTS main SUFFIX ".elf")
709+
idf_build_executable("${executable}"
710+
COMPONENTS main
711+
MAPFILE_TARGET "${executable}_mapfile")
717712

718713
if(CONFIG_APP_BUILD_GENERATE_BINARIES)
719714
# Is it possible to have a configuration where
@@ -734,6 +729,7 @@ macro(idf_project_default)
734729
TARGET app-flash
735730
NAME "app"
736731
FLASH)
732+
idf_build_generate_metadata("${executable}_binary_signed")
737733
else()
738734
idf_build_binary("${executable}"
739735
OUTPUT_FILE "${build_dir}/${executable}.bin"
@@ -750,16 +746,7 @@ macro(idf_project_default)
750746

751747
idf_create_dfu("${executable}_binary"
752748
TARGET dfu)
753-
endif()
754-
755-
# FIXME: Dependencies should be specified within the components, not in the
756-
# build system.
757-
if(CONFIG_APP_BUILD_TYPE_APP_2NDBOOT)
758-
add_dependencies(flash "partition_table_bin")
759-
endif()
760-
761-
if(CONFIG_APP_BUILD_BOOTLOADER)
762-
add_dependencies(flash "bootloader")
749+
idf_build_generate_metadata("${executable}_binary")
763750
endif()
764751

765752
idf_build_generate_flasher_args()
@@ -779,8 +766,33 @@ macro(idf_project_default)
779766
TARGET uf2-app
780767
APP_ONLY)
781768

782-
idf_build_generate_metadata("${executable}")
769+
if(TARGET "${executable}_mapfile")
770+
idf_create_size_report("${executable}_mapfile"
771+
TARGET size)
772+
endif()
773+
endfunction()
783774

784-
unset(build_dir)
785-
unset(executable)
775+
#[[api
776+
.. cmakev2:macro:: idf_project_default
777+
778+
.. code-block:: cmake
779+
780+
idf_project_default()
781+
782+
Create a default project executable based on the main component and its
783+
transitive dependencies. The executable name is derived from the
784+
``PROJECT_NAME`` variable, which by default uses the ``CMAKE_PROJECT_NAME``
785+
value specified in the CMake's ``project()`` call.
786+
787+
Generate the binary image for the executable, signed or unsigned based on
788+
the configuration, and add flash targets for it.
789+
#]]
790+
macro(idf_project_default)
791+
idf_project_init()
792+
# Only the idf_project_init macro needs be called within the global scope,
793+
# as it includes the project_include.cmake files and the cmake version of
794+
# the configuration. The remaining functionality of the idf_project_default
795+
# macro is implemented in a __project_default helper function to avoid
796+
# polluting the global variable space.
797+
__project_default()
786798
endmacro()

0 commit comments

Comments
 (0)