Skip to content

Commit 7204704

Browse files
refactor(esptool_py): Move flash target creation and post-build activities into separate files
This commit trims the idf_build_executable() logic and refactors flash target creation, utility target creation and post-build target creation into their own separate cmake files/or functions.
1 parent c29f473 commit 7204704

File tree

3 files changed

+113
-86
lines changed

3 files changed

+113
-86
lines changed

tools/cmake/build.cmake

Lines changed: 13 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Include additional cmake files for specific functionalities
2+
include("${CMAKE_CURRENT_LIST_DIR}/flash_targets.cmake")
3+
include("${CMAKE_CURRENT_LIST_DIR}/post_build_validation.cmake")
4+
15
# idf_build_get_property
26
#
37
# @brief Retrieve the value of the specified property related to ESP-IDF build.
@@ -776,18 +780,11 @@ function(idf_build_executable elf)
776780
# Add dependency of the build target to the executable
777781
add_dependencies(${elf} __idf_build_target)
778782

779-
# Set up esptool_py flash targets
780-
# This must be done after the executable properties are set but before the function exits
781-
# so that all components have had a chance to add their images to the phony flash targets
782-
idf_build_get_property(bootloader_build BOOTLOADER_BUILD)
783-
idf_build_get_property(esp_tee_build ESP_TEE_BUILD)
784-
idf_build_get_property(target IDF_TARGET)
785-
786783
# This is the main orchestrator for generating binaries and flash targets
787784
# It is responsible for -
788785
# - Setting up the binary generation targets
789786
# - Setting up the signed binary generation targets
790-
# - Setting up the main 'flash' target
787+
# - Setting up the main 'flash' target and generating flasher_args.json
791788
# - Setting up the app-flash and flash targets
792789
# - Setting up the app_check_size target
793790
#
@@ -798,6 +795,10 @@ function(idf_build_executable elf)
798795
# Note: We need to have this block here instead of in project.cmake because
799796
# idf_build_executable() is called directly when ESP-IDF is compiled
800797
# as a library (idf_as_lib).
798+
799+
idf_build_get_property(bootloader_build BOOTLOADER_BUILD)
800+
idf_build_get_property(esp_tee_build ESP_TEE_BUILD)
801+
801802
if(NOT bootloader_build AND NOT esp_tee_build)
802803
# All of the following logic for generating binaries and flash targets
803804
# depends on the esptool_py component. For some builds (such as those
@@ -828,88 +829,14 @@ function(idf_build_executable elf)
828829
__idf_build_secure_binary("${project_bin_unsigned}" "${project_bin}" "${signed_target_name}")
829830
endif()
830831

831-
# The following block is placed here to ensure that the application binary is added to the 'flash'
832-
# target's properties *before* __esptool_py_setup_main_flash_target is called.
833-
# This is because __esptool_py_setup_main_flash_target copies properties from the 'flash'
834-
# target to an internal '_flash_impl' target, from which the final 'flash_args' file is generated.
835-
# If the app target is not added to the 'flash' target *before* the properties are copied,
836-
# the app binary will be missing from the final 'flash_args' file.
837-
#
838-
# Set up app-flash and flash targets. The app-flash target is specifically for flashing
839-
# just the application, while the flash target is for flashing the entire system.
840-
idf_build_get_property(build_dir BUILD_DIR)
841-
idf_build_get_property(project_bin PROJECT_BIN)
842-
partition_table_get_partition_info(app_partition_offset "--partition-boot-default" "offset")
843-
esptool_py_custom_target(app-flash app "app")
844-
845-
esptool_py_flash_target_image(app-flash app "${app_partition_offset}" "${build_dir}/${project_bin}")
846-
esptool_py_flash_target_image(flash app "${app_partition_offset}" "${build_dir}/${project_bin}")
832+
# Setup flash targets and flash configuration
833+
__idf_build_setup_flash_targets()
847834

848835
# Setup utility targets such as monitor, erase_flash, merge-bin
849836
__esptool_py_setup_utility_targets()
850837

851-
# Setup the main flash target and dependencies
852-
__esptool_py_setup_main_flash_target()
853-
854-
# Generate flasher_args.json for tools that need it. The variables below are used
855-
# in configuring the template flasher_args.json.in.
856-
# Some of the variables (flash mode, size, frequency) are set as esptool_py component's properties.
857-
858-
idf_build_get_property(target IDF_TARGET)
859-
set(ESPTOOLPY_CHIP "${target}")
860-
set(ESPTOOLPY_BEFORE "${CONFIG_ESPTOOLPY_BEFORE}")
861-
set(ESPTOOLPY_AFTER "${CONFIG_ESPTOOLPY_AFTER}")
862-
if(CONFIG_ESPTOOLPY_NO_STUB)
863-
set(ESPTOOLPY_WITH_STUB false)
864-
else()
865-
set(ESPTOOLPY_WITH_STUB true)
866-
endif()
867-
868-
if(CONFIG_SECURE_BOOT OR CONFIG_SECURE_FLASH_ENC_ENABLED)
869-
# If security enabled then override post flash option
870-
set(ESPTOOLPY_AFTER "no_reset")
871-
endif()
872-
873-
idf_component_get_property(ESPFLASHMODE esptool_py ESPFLASHMODE)
874-
idf_component_get_property(ESPFLASHFREQ esptool_py ESPFLASHFREQ)
875-
idf_component_get_property(ESPFLASHSIZE esptool_py ESPFLASHSIZE)
876-
idf_component_get_property(esptool_py_dir esptool_py COMPONENT_DIR)
877-
878-
# Generate flasher args files
879-
file(READ "${esptool_py_dir}/flasher_args.json.in" flasher_args_content)
880-
string(CONFIGURE "${flasher_args_content}" flasher_args_content)
881-
882-
# We need to create a flasher_args.json.in to create the final flasher_args.json
883-
# because CMake only resolves generator expressions in the file_generate command
884-
# with the INPUT keyword during the generation phase.
885-
file_generate("${build_dir}/flasher_args.json.in"
886-
CONTENT "${flasher_args_content}")
887-
file_generate("${build_dir}/flasher_args.json"
888-
INPUT "${build_dir}/flasher_args.json.in")
889-
890-
# Create the following post-build targets after __idf_build_binary() is called to ensure that the
891-
# app target is available.
892-
893-
# If anti-rollback option is set then factory partition should not be in Partition Table.
894-
# In this case, should be used the partition table with two ota app without the factory.
895-
partition_table_get_partition_info(factory_offset
896-
"--partition-type app --partition-subtype factory" "offset")
897-
partition_table_get_partition_info(test_offset "--partition-type app --partition-subtype test" "offset")
898-
if(CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK AND (factory_offset OR test_offset))
899-
fail_at_build_time(check_table_contents "\
900-
ERROR: Anti-rollback option is enabled. Partition table should \
901-
consist of two ota app without factory or test partitions.")
902-
add_dependencies(app check_table_contents)
903-
endif()
904-
905-
if(CONFIG_APP_BUILD_TYPE_APP_2NDBOOT)
906-
# Generate app_check_size_command target to check the app size against the partition table parameters
907-
partition_table_add_check_size_target(app_check_size
908-
DEPENDS gen_project_binary
909-
BINARY_PATH "${build_dir}/${project_bin}"
910-
PARTITION_TYPE app)
911-
add_dependencies(app app_check_size)
912-
endif()
838+
# Setup post-build validation checks
839+
__idf_build_setup_post_build_validation()
913840
endif() # if(TARGET idf::esptool_py AND CONFIG_APP_BUILD_GENERATE_BINARIES)
914841
endif() # if(NOT bootloader_build AND NOT esp_tee_build)
915842
endfunction()

tools/cmake/flash_targets.cmake

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# flash_targets.cmake
2+
#
3+
# Functions for setting up flash-related targets and configuration
4+
5+
#
6+
# Setup flash targets for the application
7+
#
8+
function(__idf_build_setup_flash_targets)
9+
# Set up app-flash and flash targets. The app-flash target is specifically for flashing
10+
# just the application, while the flash target is for flashing the entire system.
11+
idf_build_get_property(build_dir BUILD_DIR)
12+
idf_build_get_property(project_bin PROJECT_BIN)
13+
partition_table_get_partition_info(app_partition_offset "--partition-boot-default" "offset")
14+
esptool_py_custom_target(app-flash app "app")
15+
16+
esptool_py_flash_target_image(app-flash app "${app_partition_offset}" "${build_dir}/${project_bin}")
17+
esptool_py_flash_target_image(flash app "${app_partition_offset}" "${build_dir}/${project_bin}")
18+
19+
# Setup the main flash target and dependencies
20+
__esptool_py_setup_main_flash_target()
21+
22+
# Generate flasher_args.json configuration files
23+
__idf_build_generate_flasher_args()
24+
endfunction()
25+
26+
#
27+
# Generate flasher_args.json configuration files
28+
#
29+
function(__idf_build_generate_flasher_args)
30+
# Generate flasher_args.json for tools that need it. The variables below are used
31+
# in configuring the template flasher_args.json.in.
32+
# Some of the variables (flash mode, size, frequency) are set as esptool_py component's properties.
33+
34+
idf_build_get_property(target IDF_TARGET)
35+
set(ESPTOOLPY_CHIP "${target}")
36+
set(ESPTOOLPY_BEFORE "${CONFIG_ESPTOOLPY_BEFORE}")
37+
set(ESPTOOLPY_AFTER "${CONFIG_ESPTOOLPY_AFTER}")
38+
if(CONFIG_ESPTOOLPY_NO_STUB)
39+
set(ESPTOOLPY_WITH_STUB false)
40+
else()
41+
set(ESPTOOLPY_WITH_STUB true)
42+
endif()
43+
44+
if(CONFIG_SECURE_BOOT OR CONFIG_SECURE_FLASH_ENC_ENABLED)
45+
# If security enabled then override post flash option
46+
set(ESPTOOLPY_AFTER "no_reset")
47+
endif()
48+
49+
idf_component_get_property(ESPFLASHMODE esptool_py ESPFLASHMODE)
50+
idf_component_get_property(ESPFLASHFREQ esptool_py ESPFLASHFREQ)
51+
idf_component_get_property(ESPFLASHSIZE esptool_py ESPFLASHSIZE)
52+
idf_component_get_property(esptool_py_dir esptool_py COMPONENT_DIR)
53+
54+
# Generate flasher args files
55+
idf_build_get_property(build_dir BUILD_DIR)
56+
file(READ "${esptool_py_dir}/flasher_args.json.in" flasher_args_content)
57+
string(CONFIGURE "${flasher_args_content}" flasher_args_content)
58+
59+
# We need to create a flasher_args.json.in to create the final flasher_args.json
60+
# because CMake only resolves generator expressions in the file_generate command
61+
# with the INPUT keyword during the generation phase.
62+
file_generate("${build_dir}/flasher_args.json.in"
63+
CONTENT "${flasher_args_content}")
64+
file_generate("${build_dir}/flasher_args.json"
65+
INPUT "${build_dir}/flasher_args.json.in")
66+
endfunction()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# post_build_validation.cmake
2+
#
3+
# Functions for post-build validation and checks
4+
5+
#
6+
# Setup post-build validation checks
7+
#
8+
function(__idf_build_setup_post_build_validation)
9+
# Create the following post-build targets after __idf_build_binary() is called to ensure that the
10+
# app target is available.
11+
12+
# If anti-rollback option is set then factory partition should not be in Partition Table.
13+
# In this case, should be used the partition table with two ota app without the factory.
14+
partition_table_get_partition_info(factory_offset
15+
"--partition-type app --partition-subtype factory" "offset")
16+
partition_table_get_partition_info(test_offset "--partition-type app --partition-subtype test" "offset")
17+
if(CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK AND (factory_offset OR test_offset))
18+
fail_at_build_time(check_table_contents "\
19+
ERROR: Anti-rollback option is enabled. Partition table should \
20+
consist of two ota app without factory or test partitions.")
21+
add_dependencies(app check_table_contents)
22+
endif()
23+
24+
if(CONFIG_APP_BUILD_TYPE_APP_2NDBOOT)
25+
# Generate app_check_size_command target to check the app size against the partition table parameters
26+
idf_build_get_property(build_dir BUILD_DIR)
27+
idf_build_get_property(project_bin PROJECT_BIN)
28+
partition_table_add_check_size_target(app_check_size
29+
DEPENDS gen_project_binary
30+
BINARY_PATH "${build_dir}/${project_bin}"
31+
PARTITION_TYPE app)
32+
add_dependencies(app app_check_size)
33+
endif()
34+
endfunction()

0 commit comments

Comments
 (0)