From e5f35bc2d73a16606f389ff20930105fb81c0455 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Tue, 18 Nov 2025 08:53:41 +0530 Subject: [PATCH] feat(cmakev2): Add uf2 target support for cmakev2 This commit adds support for uf2 targets for cmake2. The following changes have been made: - Adds a new tools/cmakev2/uf2.cmake. - Adds the idf_create_uf2() function to create the uf2 targets. This function now takes the executable as an argument thus allowing the uf2 target to be created per-executable. - idf_project_default() is updated to create the uf2 targets. --- tools/cmakev2/idf.cmake | 1 + tools/cmakev2/project.cmake | 6 +++ tools/cmakev2/uf2.cmake | 82 +++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 tools/cmakev2/uf2.cmake diff --git a/tools/cmakev2/idf.cmake b/tools/cmakev2/idf.cmake index 73d94648af..50b4368e6a 100644 --- a/tools/cmakev2/idf.cmake +++ b/tools/cmakev2/idf.cmake @@ -35,6 +35,7 @@ include(manager) include(compat) include(ldgen) include(dfu) +include(uf2) 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 c4bfde3c15..eaf4d6043a 100644 --- a/tools/cmakev2/project.cmake +++ b/tools/cmakev2/project.cmake @@ -773,6 +773,12 @@ macro(idf_project_default) idf_create_save_defconfig() + idf_create_uf2("${executable}" + TARGET uf2) + idf_create_uf2("${executable}" + TARGET uf2-app + APP_ONLY) + idf_build_generate_metadata("${executable}") unset(build_dir) diff --git a/tools/cmakev2/uf2.cmake b/tools/cmakev2/uf2.cmake new file mode 100644 index 0000000000..efc6d28887 --- /dev/null +++ b/tools/cmakev2/uf2.cmake @@ -0,0 +1,82 @@ +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 + +#[[ +.. cmakev2:function:: idf_create_uf2 + + .. code-block:: cmake + + idf_create_uf2( + TARGET + [APP_ONLY]) + + *executable[in]* + + Executable target for which to create UF2 target. + + *TARGET[in]* + + Name of the UF2 generation target to be created (e.g., "uf2", "uf2-app"). + + *APP_ONLY[option]* + + If specified, creates a UF2 binary file with only the application binary + (uses --bin app option). If not specified, creates a UF2 binary file with + all binaries from flasher_args.json. + + Create a UF2 (USB Flashing Format) target for the specified executable. + + Example usage: + idf_create_uf2(myapp TARGET uf2) # All binaries + idf_create_uf2(myapp TARGET uf2-app APP_ONLY) # App binary only +#]] +function(idf_create_uf2 executable) + set(options APP_ONLY) + 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() + + if(NOT TARGET "${executable}") + idf_die("Executable '${executable}' is not a cmake target") + endif() + + # Get build properties + idf_build_get_property(python PYTHON) + idf_build_get_property(idf_path IDF_PATH) + idf_build_get_property(build_dir BUILD_DIR) + idf_build_get_property(target IDF_TARGET) + + # Path to UF2 output file + set(uf2_output_file "${build_dir}/${ARG_TARGET}.bin") + + # Build UF2 command and arguments (matching cmakev1 pattern) + set(UF2_ARGS --json "${build_dir}/flasher_args.json") + set(UF2_CMD ${python} "${idf_path}/tools/mkuf2.py" write --chip ${target}) + + # Add output file and --bin app if APP_ONLY is specified + set(uf2_args_list "${UF2_ARGS};-o;${uf2_output_file}") + if(ARG_APP_ONLY) + list(APPEND uf2_args_list --bin app) + set(comment_msg "Generating UF2 app binary for ${executable}") + else() + set(comment_msg "Generating UF2 binary for ${executable}") + endif() + + # Create UF2 target (matching cmakev1 pattern using run_uf2_cmds.cmake) + add_custom_target(${ARG_TARGET} + COMMAND ${CMAKE_COMMAND} + -D "IDF_PATH=${idf_path}" + -D "UF2_CMD=${UF2_CMD}" + -D "UF2_ARGS=${uf2_args_list}" + -P "${idf_path}/tools/cmake/run_uf2_cmds.cmake" + USES_TERMINAL + VERBATIM + COMMENT "${comment_msg}" + ) + + idf_msg("Created UF2 target: ${ARG_TARGET}") +endfunction()