Skip to content

Commit 94809d6

Browse files
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.
1 parent a609d5b commit 94809d6

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

tools/cmakev2/idf.cmake

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

tools/cmakev2/project.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,12 @@ macro(idf_project_default)
773773

774774
idf_create_save_defconfig()
775775

776+
idf_create_uf2("${executable}"
777+
TARGET uf2)
778+
idf_create_uf2("${executable}"
779+
TARGET uf2-app
780+
APP_ONLY)
781+
776782
idf_build_generate_metadata("${executable}")
777783

778784
unset(build_dir)

tools/cmakev2/uf2.cmake

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
#[[
5+
.. cmakev2:function:: idf_create_uf2
6+
7+
.. code-block:: cmake
8+
9+
idf_create_uf2(<executable>
10+
TARGET <target>
11+
[APP_ONLY])
12+
13+
*executable[in]*
14+
15+
Executable target for which to create UF2 target.
16+
17+
*TARGET[in]*
18+
19+
Name of the UF2 generation target to be created (e.g., "uf2", "uf2-app").
20+
21+
*APP_ONLY[option]*
22+
23+
If specified, creates a UF2 binary file with only the application binary
24+
(uses --bin app option). If not specified, creates a UF2 binary file with
25+
all binaries from flasher_args.json.
26+
27+
Create a UF2 (USB Flashing Format) target for the specified executable.
28+
29+
Example usage:
30+
idf_create_uf2(myapp TARGET uf2) # All binaries
31+
idf_create_uf2(myapp TARGET uf2-app APP_ONLY) # App binary only
32+
#]]
33+
function(idf_create_uf2 executable)
34+
set(options APP_ONLY)
35+
set(one_value TARGET)
36+
set(multi_value)
37+
cmake_parse_arguments(ARG "${options}" "${one_value}" "${multi_value}" ${ARGN})
38+
39+
if(NOT DEFINED ARG_TARGET)
40+
idf_die("TARGET option is required")
41+
endif()
42+
43+
if(NOT TARGET "${executable}")
44+
idf_die("Executable '${executable}' is not a cmake target")
45+
endif()
46+
47+
# Get build properties
48+
idf_build_get_property(python PYTHON)
49+
idf_build_get_property(idf_path IDF_PATH)
50+
idf_build_get_property(build_dir BUILD_DIR)
51+
idf_build_get_property(target IDF_TARGET)
52+
53+
# Path to UF2 output file
54+
set(uf2_output_file "${build_dir}/${ARG_TARGET}.bin")
55+
56+
# Build UF2 command and arguments (matching cmakev1 pattern)
57+
set(UF2_ARGS --json "${build_dir}/flasher_args.json")
58+
set(UF2_CMD ${python} "${idf_path}/tools/mkuf2.py" write --chip ${target})
59+
60+
# Add output file and --bin app if APP_ONLY is specified
61+
set(uf2_args_list "${UF2_ARGS};-o;${uf2_output_file}")
62+
if(ARG_APP_ONLY)
63+
list(APPEND uf2_args_list --bin app)
64+
set(comment_msg "Generating UF2 app binary for ${executable}")
65+
else()
66+
set(comment_msg "Generating UF2 binary for ${executable}")
67+
endif()
68+
69+
# Create UF2 target (matching cmakev1 pattern using run_uf2_cmds.cmake)
70+
add_custom_target(${ARG_TARGET}
71+
COMMAND ${CMAKE_COMMAND}
72+
-D "IDF_PATH=${idf_path}"
73+
-D "UF2_CMD=${UF2_CMD}"
74+
-D "UF2_ARGS=${uf2_args_list}"
75+
-P "${idf_path}/tools/cmake/run_uf2_cmds.cmake"
76+
USES_TERMINAL
77+
VERBATIM
78+
COMMENT "${comment_msg}"
79+
)
80+
81+
idf_msg("Created UF2 target: ${ARG_TARGET}")
82+
endfunction()

0 commit comments

Comments
 (0)