mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
refactor(esptool_py): Move binary generation to project level and add utility functions
This commit refactors the esptool_py component to provide utility functions for binary file generation targets instead of creating the targets. Binary generation targets are now moved to the respective projects. The following changes were done in this commit: - Added __idf_build_binary() function to esptool_py to create the binary file generation target. - Added __idf_build_secure_binary() as the secure boot equivalent of the above function. - Top level project build now creates its own binary targets in idf_build_executable() in build.cmake. - Bootloader and esp_tee subprojects create their binary file generation targets in their respective CMakeLists.txt files. - All post-build targets such as the app_size_check target are now created by the respective projects and not esptool_py. - General clean-up of the esptool_py cmake files.
This commit is contained in:
@@ -17,16 +17,6 @@ if(NOT BOOTLOADER_BUILD)
|
||||
|
||||
|
||||
|
||||
# If anti-rollback option is set then factory partition should not be in Partition Table.
|
||||
# In this case, should be used the partition table with two ota app without the factory.
|
||||
partition_table_get_partition_info(factory_offset "--partition-type app --partition-subtype factory" "offset")
|
||||
partition_table_get_partition_info(test_offset "--partition-type app --partition-subtype test" "offset")
|
||||
if(CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK AND (factory_offset OR test_offset))
|
||||
fail_at_build_time(check_table_contents "\
|
||||
ERROR: Anti-rollback option is enabled. Partition table should \
|
||||
consist of two ota app without factory or test partitions.")
|
||||
add_dependencies(app check_table_contents)
|
||||
endif()
|
||||
|
||||
# Generate flasher_args.json for tools that need it. The variables below are used
|
||||
# in configuring the template flasher_args.json.in.
|
||||
@@ -54,31 +44,5 @@ consist of two ota app without factory or test partitions.")
|
||||
CONTENT "${flasher_args_content}")
|
||||
file_generate("${CMAKE_BINARY_DIR}/flasher_args.json"
|
||||
INPUT "${CMAKE_CURRENT_BINARY_DIR}/flasher_args.json.in")
|
||||
if(CONFIG_APP_BUILD_TYPE_APP_2NDBOOT)
|
||||
# Generate app_check_size_command target to check the app size against the partition table parameters
|
||||
partition_table_add_check_size_target(app_check_size
|
||||
DEPENDS gen_project_binary
|
||||
BINARY_PATH "${build_dir}/${PROJECT_BIN}"
|
||||
PARTITION_TYPE app)
|
||||
add_dependencies(app app_check_size)
|
||||
endif()
|
||||
endif()
|
||||
endif() # NOT BOOTLOADER_BUILD
|
||||
|
||||
if(BOOTLOADER_BUILD)
|
||||
# Generate bootloader post-build check of the bootloader size against the offset
|
||||
partition_table_add_check_bootloader_size_target(bootloader_check_size
|
||||
DEPENDS gen_project_binary
|
||||
BOOTLOADER_BINARY_PATH "${build_dir}/${PROJECT_BIN}"
|
||||
RESULT bootloader_check_size_command)
|
||||
add_dependencies(app bootloader_check_size) # note: in the subproject, so the target is 'app'...
|
||||
|
||||
if(CONFIG_SECURE_BOOT_V2_ENABLED AND CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
|
||||
# Check the size of the bootloader + signature block.
|
||||
partition_table_add_check_bootloader_size_target(bootloader_check_size_signed
|
||||
DEPENDS gen_signed_bootloader
|
||||
BOOTLOADER_BINARY_PATH "${build_dir}/${PROJECT_BIN}"
|
||||
RESULT bootloader_check_size_signed_command)
|
||||
add_dependencies(app bootloader_check_size_signed) # note: in the subproject, so the target is 'app'...
|
||||
endif()
|
||||
endif()
|
||||
|
@@ -112,79 +112,6 @@ idf_build_get_property(build_dir BUILD_DIR)
|
||||
idf_build_get_property(elf_name EXECUTABLE_NAME GENERATOR_EXPRESSION)
|
||||
idf_build_get_property(elf EXECUTABLE GENERATOR_EXPRESSION)
|
||||
|
||||
if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES AND NOT non_os_build)
|
||||
set(unsigned_project_binary "${elf_name}-unsigned.bin")
|
||||
else()
|
||||
set(unsigned_project_binary "${elf_name}.bin")
|
||||
endif()
|
||||
|
||||
set(PROJECT_BIN "${elf_name}.bin")
|
||||
|
||||
#
|
||||
# Add 'app.bin' target - generates with elf2image
|
||||
#
|
||||
if(CONFIG_APP_BUILD_GENERATE_BINARIES)
|
||||
add_custom_command(OUTPUT "${build_dir}/.bin_timestamp"
|
||||
COMMAND ${ESPTOOLPY} elf2image ${esptool_elf2image_args}
|
||||
-o "${build_dir}/${unsigned_project_binary}" "$<TARGET_FILE:$<GENEX_EVAL:${elf}>>"
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Generated ${build_dir}/${unsigned_project_binary}"
|
||||
COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${unsigned_project_binary}" > "${build_dir}/.bin_timestamp"
|
||||
DEPENDS "$<TARGET_FILE:$<GENEX_EVAL:${elf}>>"
|
||||
VERBATIM
|
||||
WORKING_DIRECTORY ${build_dir}
|
||||
COMMENT "Generating binary image from built executable"
|
||||
)
|
||||
add_custom_target(gen_project_binary DEPENDS "${build_dir}/.bin_timestamp")
|
||||
endif()
|
||||
|
||||
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
APPEND PROPERTY ADDITIONAL_CLEAN_FILES
|
||||
"${build_dir}/${unsigned_project_binary}"
|
||||
)
|
||||
|
||||
if(CONFIG_APP_BUILD_GENERATE_BINARIES)
|
||||
add_custom_target(app ALL DEPENDS gen_project_binary)
|
||||
endif()
|
||||
|
||||
if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
|
||||
set(secure_boot_version "1")
|
||||
elseif(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME OR CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME)
|
||||
set(secure_boot_version "2")
|
||||
endif()
|
||||
|
||||
if(NOT non_os_build AND CONFIG_SECURE_SIGNED_APPS)
|
||||
if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
|
||||
# for locally signed secure boot image, add a signing step to get from unsigned app to signed app
|
||||
get_filename_component(secure_boot_signing_key "${CONFIG_SECURE_BOOT_SIGNING_KEY}"
|
||||
ABSOLUTE BASE_DIR "${project_dir}")
|
||||
add_custom_command(OUTPUT "${build_dir}/.signed_bin_timestamp"
|
||||
COMMAND ${ESPSECUREPY} sign_data --version ${secure_boot_version} --keyfile ${secure_boot_signing_key}
|
||||
-o "${build_dir}/${PROJECT_BIN}" "${build_dir}/${unsigned_project_binary}"
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Generated signed binary image ${build_dir}/${PROJECT_BIN}"
|
||||
"from ${build_dir}/${unsigned_project_binary}"
|
||||
COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${PROJECT_BIN}" > "${build_dir}/.signed_bin_timestamp"
|
||||
DEPENDS "${build_dir}/.bin_timestamp"
|
||||
VERBATIM
|
||||
COMMENT "Generating signed binary image"
|
||||
)
|
||||
add_custom_target(gen_signed_project_binary DEPENDS "${build_dir}/.signed_bin_timestamp")
|
||||
add_dependencies(gen_project_binary gen_signed_project_binary)
|
||||
|
||||
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
APPEND PROPERTY ADDITIONAL_CLEAN_FILES
|
||||
"${build_dir}/${PROJECT_BIN}"
|
||||
)
|
||||
else()
|
||||
string(REPLACE ";" " " espsecurepy "${ESPSECUREPY}")
|
||||
add_custom_command(TARGET app POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E echo
|
||||
"App built but not signed. Sign app before flashing"
|
||||
COMMAND ${CMAKE_COMMAND} -E echo
|
||||
"\t${espsecurepy} sign_data --keyfile KEYFILE --version ${secure_boot_version} \
|
||||
${build_dir}/${PROJECT_BIN}"
|
||||
VERBATIM)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_custom_target(erase_flash
|
||||
COMMAND ${CMAKE_COMMAND}
|
||||
@@ -781,6 +708,147 @@ function(__ensure_esptool_py_setup)
|
||||
endfunction()
|
||||
|
||||
|
||||
# __idf_build_binary
|
||||
#
|
||||
# @brief Sets up the primary target for generating a .bin file from an .elf file.
|
||||
#
|
||||
# This function creates the custom command and target required to generate a
|
||||
# project binary (`.bin`) file from the final `.elf` executable. It uses `esptool.py
|
||||
# elf2image` to perform the conversion and manages dependencies to ensure the
|
||||
# binary is regenerated whenever the ELF file changes.
|
||||
#
|
||||
# @param[in] OUTPUT_BIN_FILENAME The name of the output binary file to generate.
|
||||
# @param[in] TARGET_NAME The unique name for the custom target that
|
||||
# generates the binary.
|
||||
function(__idf_build_binary OUTPUT_BIN_FILENAME TARGET_NAME)
|
||||
__ensure_esptool_py_setup()
|
||||
|
||||
idf_build_get_property(build_dir BUILD_DIR)
|
||||
idf_build_get_property(elf EXECUTABLE GENERATOR_EXPRESSION)
|
||||
idf_component_get_property(esptool_py_cmd esptool_py ESPTOOLPY_CMD)
|
||||
|
||||
# Get esptool.py arguments for elf2image target
|
||||
idf_component_get_property(esptool_elf2image_args esptool_py ESPTOOL_PY_ELF2IMAGE_ARGS)
|
||||
|
||||
# Create a custom command and target to generate binary from elf file
|
||||
add_custom_command(OUTPUT "${build_dir}/.bin_timestamp"
|
||||
COMMAND ${esptool_py_cmd} elf2image ${esptool_elf2image_args}
|
||||
-o "${build_dir}/${OUTPUT_BIN_FILENAME}" "$<TARGET_FILE:$<GENEX_EVAL:${elf}>>"
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Generated ${build_dir}/${OUTPUT_BIN_FILENAME}"
|
||||
COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${OUTPUT_BIN_FILENAME}" > "${build_dir}/.bin_timestamp"
|
||||
DEPENDS "$<TARGET_FILE:$<GENEX_EVAL:${elf}>>"
|
||||
VERBATIM
|
||||
WORKING_DIRECTORY ${build_dir}
|
||||
COMMENT "Generating binary image from built executable"
|
||||
)
|
||||
# Create a custom target to generate the binary file
|
||||
add_custom_target(${TARGET_NAME} DEPENDS "${build_dir}/.bin_timestamp")
|
||||
|
||||
# We need to create a gen_project_binary target for backward compatibility
|
||||
# since many other components depend on it. Add the new target as a dependency
|
||||
# to the gen_project_binary target.
|
||||
if(NOT TARGET gen_project_binary)
|
||||
add_custom_target(gen_project_binary DEPENDS ${TARGET_NAME})
|
||||
else()
|
||||
add_dependencies(gen_project_binary ${TARGET_NAME})
|
||||
endif()
|
||||
|
||||
# Add an 'app' alias that is part of the default build
|
||||
if(NOT TARGET app)
|
||||
add_custom_target(app ALL DEPENDS gen_project_binary)
|
||||
else()
|
||||
add_dependencies(app gen_project_binary)
|
||||
endif()
|
||||
|
||||
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
APPEND PROPERTY ADDITIONAL_CLEAN_FILES
|
||||
"${build_dir}/${OUTPUT_BIN_FILENAME}"
|
||||
)
|
||||
endfunction()
|
||||
|
||||
# __idf_build_secure_binary
|
||||
#
|
||||
# @brief Sets up targets for generating a signed binary for Secure Boot.
|
||||
#
|
||||
# If Secure Boot is enabled, this function adds a custom command to sign the
|
||||
# previously generated application binary using `espsecure.py`. It creates a
|
||||
# target that depends on the unsigned binary and produces a signed one, which
|
||||
# is required for the bootloader to authenticate the application.
|
||||
#
|
||||
# @param[in] UNSIGNED_BIN_FILENAME The name of the unsigned input binary file.
|
||||
# @param[in] SIGNED_BIN_FILENAME The name of the signed output binary file.
|
||||
# @param[in] TARGET_NAME The unique name for the custom target that
|
||||
# generates the signed binary.
|
||||
# @param[in, optional] KEYFILE Path to the keyfile for signing.
|
||||
# @param[in, optional] COMMENT Custom message to display during build.
|
||||
function(__idf_build_secure_binary UNSIGNED_BIN_FILENAME SIGNED_BIN_FILENAME TARGET_NAME)
|
||||
cmake_parse_arguments(arg "" "KEYFILE;COMMENT" "" ${ARGN})
|
||||
|
||||
__ensure_esptool_py_setup()
|
||||
|
||||
idf_build_get_property(build_dir BUILD_DIR)
|
||||
idf_component_get_property(espsecure_py_cmd esptool_py ESPSECUREPY_CMD)
|
||||
|
||||
if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME)
|
||||
set(secure_boot_version "1")
|
||||
elseif(CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME OR CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME)
|
||||
set(secure_boot_version "2")
|
||||
endif()
|
||||
|
||||
if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES)
|
||||
if(arg_KEYFILE)
|
||||
# If a keyfile is provided, use it for signing.
|
||||
set(secure_boot_signing_key "${arg_KEYFILE}")
|
||||
else()
|
||||
# for locally signed secure boot image, add a signing step to get from unsigned app to signed app
|
||||
idf_build_get_property(project_dir PROJECT_DIR)
|
||||
get_filename_component(secure_boot_signing_key "${CONFIG_SECURE_BOOT_SIGNING_KEY}"
|
||||
ABSOLUTE BASE_DIR "${project_dir}")
|
||||
endif()
|
||||
|
||||
if(arg_COMMENT)
|
||||
set(comment_text "${arg_COMMENT}")
|
||||
else()
|
||||
set(comment_text "Generating signed binary image")
|
||||
endif()
|
||||
|
||||
add_custom_command(OUTPUT "${build_dir}/.signed_bin_timestamp"
|
||||
COMMAND ${espsecure_py_cmd} sign_data
|
||||
--version ${secure_boot_version} --keyfile "${secure_boot_signing_key}"
|
||||
-o "${build_dir}/${SIGNED_BIN_FILENAME}" "${build_dir}/${UNSIGNED_BIN_FILENAME}"
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Generated signed binary image ${build_dir}/${SIGNED_BIN_FILENAME}"
|
||||
"from ${build_dir}/${UNSIGNED_BIN_FILENAME}"
|
||||
COMMAND ${CMAKE_COMMAND} -E md5sum "${build_dir}/${SIGNED_BIN_FILENAME}"
|
||||
> "${build_dir}/.signed_bin_timestamp"
|
||||
DEPENDS "${build_dir}/.bin_timestamp"
|
||||
VERBATIM
|
||||
COMMENT "${comment_text}"
|
||||
)
|
||||
add_custom_target(${TARGET_NAME} DEPENDS "${build_dir}/.signed_bin_timestamp")
|
||||
|
||||
# Add the new target as a dependency to the gen_project_binary target.
|
||||
if(NOT TARGET gen_project_binary)
|
||||
add_custom_target(gen_project_binary DEPENDS ${TARGET_NAME})
|
||||
else()
|
||||
add_dependencies(gen_project_binary ${TARGET_NAME})
|
||||
endif()
|
||||
|
||||
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
APPEND PROPERTY ADDITIONAL_CLEAN_FILES
|
||||
"${build_dir}/${SIGNED_BIN_FILENAME}"
|
||||
)
|
||||
else()
|
||||
string(REPLACE ";" " " espsecurepy "${espsecure_py_cmd}")
|
||||
add_custom_command(TARGET app POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E echo
|
||||
"App built but not signed. Sign app before flashing"
|
||||
COMMAND ${CMAKE_COMMAND} -E echo
|
||||
"\t${espsecurepy} sign_data --keyfile KEYFILE --version ${secure_boot_version} \
|
||||
${build_dir}/${UNSIGNED_BIN_FILENAME}"
|
||||
VERBATIM)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# __esptool_py_setup_main_flash_target
|
||||
#
|
||||
# @brief Sets up the main `flash` target and its dependencies.
|
||||
|
Reference in New Issue
Block a user