From a90bd6e7d3b8a7668a7c1339b9c19594573b3ad5 Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Thu, 28 Aug 2025 13:35:59 +0200 Subject: [PATCH] feat(cmakev2/build): add idf_build_executable function Create an empty source file for the add_executable target, along with a library containing specified components, and then create an executable. This approach allows for the easy creation of multiple executables by specifying the component with the app_main function, as demonstrated in the test. ``` idf_build_executable(fatfs_example COMPONENTS fatfs_example) idf_build_executable(hello_world_example COMPONENTS hello_world_example) ``` Signed-off-by: Frantisek Hrbata --- tools/cmakev2/build.cmake | 56 +++++++++++++++++++++++++++++++ tools/cmakev2/test/CMakeLists.txt | 13 ++----- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/tools/cmakev2/build.cmake b/tools/cmakev2/build.cmake index 12941e18b41..c77140e950c 100644 --- a/tools/cmakev2/build.cmake +++ b/tools/cmakev2/build.cmake @@ -378,3 +378,59 @@ function(idf_build_library library) endforeach() endforeach() endfunction() + +#[[api +.. cmakev2:function:: idf_build_executable + + .. code-block:: cmake + + idf_build_executable( + [COMPONENTS ...] + [NAME ] + [SUFFIX ]) + + :executable[in]: Name of the executable target to be created. + :COMPONENTS[in,opt]: List of component names to add to the library. + :NAME[in,opt]: Optional preferred generated binary name. If not provided, + the ``executable`` name is used. + :SUFFIX[in,opt]: Optional ``executable`` suffix. + + Create a new executable target using the name specified in the + ``executable`` argument, and link it to the library created with the + component names provided in the ``COMPONENTS`` option. If the ``COMPONENTS`` + option is not set, all discovered components are added to the library. + Optinaly set the executable name and suffix. +#]] +function(idf_build_executable executable) + set(options) + set(one_value NAME SUFFIX) + set(multi_value COMPONENTS) + cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN}) + + if(NOT DEFINED ARG_NAME) + set(ARG_NAME "${executable}") + endif() + + if(NOT DEFINED ARG_SUFFIX) + set(ARG_SUFFIX ".elf") + endif() + + idf_build_get_property(build_dir BUILD_DIR) + + set(library "library_${executable}") + idf_build_library(${library} COMPONENTS "${ARG_COMPONENTS}") + + set(executable_src ${CMAKE_BINARY_DIR}/executable_${executable}.c) + file(TOUCH "${executable_src}") + add_executable(${executable} "${executable_src}") + + if(ARG_NAME) + set_target_properties(${executable} PROPERTIES OUTPUT_NAME ${ARG_NAME}) + endif() + + if(ARG_SUFFIX) + set_target_properties(${executable} PROPERTIES SUFFIX ${ARG_SUFFIX}) + endif() + + target_link_libraries(${executable} PRIVATE ${library}) +endfunction() diff --git a/tools/cmakev2/test/CMakeLists.txt b/tools/cmakev2/test/CMakeLists.txt index a1e29563c25..3cbd4db04da 100644 --- a/tools/cmakev2/test/CMakeLists.txt +++ b/tools/cmakev2/test/CMakeLists.txt @@ -208,17 +208,8 @@ endfunction() # cmake --build build/ --target hello_world_example # cmake --build build/ --target fatfs_example function(test_executable) - idf_build_get_property(build_dir BUILD_DIR) - file(TOUCH "${build_dir}/test_executable.c") - - add_executable(fatfs_example "${build_dir}/test_executable.c") - idf_build_library(fatfs_lib COMPONENTS fatfs_example) - target_link_libraries(fatfs_example PRIVATE fatfs_lib) - - add_executable(hello_world_example "${build_dir}/test_executable.c") - idf_build_library(hello_world_lib COMPONENTS hello_world_example) - target_link_libraries(hello_world_example PRIVATE hello_world_lib) - + idf_build_executable(fatfs_example COMPONENTS fatfs_example) + idf_build_executable(hello_world_example COMPONENTS hello_world_example) endfunction() # Run tests