feat(cmakev2/utilities): add target_add_binary_data function

Add binary data into the build target by converting it into a generated
source file, which is then compiled into a binary object as part of the
build process.

Signed-off-by: Frantisek Hrbata <frantisek.hrbata@espressif.com>
This commit is contained in:
Frantisek Hrbata
2025-08-19 13:55:32 +02:00
parent 37290f5e9e
commit 1b808d1df6

View File

@@ -607,3 +607,54 @@ function(add_deprecated_target_alias old_target new_target)
)
add_dependencies(${old_target} ${new_target})
endfunction()
#[[
target_add_binary_data(<target> <embed_file> <embed_type>
[RENAME_TO <symbol>])
[DEPENDS <dependency>...])
:target[in]: Attach the source file with embedded data to the specified
target.
:embed_file[in]: File containing the embedded data.
:embed_type[in]: BINARY or TEXT
:RENAME_TO[in,opt]: Use the given symbol name for the embedded data. If no
symbol name is provided, the embed_file file name will
be used instead.
:DEPENDS[in,opt]: List of additional dependencies for the generated file
containing embedded data.
Add binary data into the build target by converting it into a generated
source file, which is then compiled into a binary object as part of the
build process.
#]]
function(target_add_binary_data target embed_file embed_type)
cmake_parse_arguments(_ "" "RENAME_TO" "DEPENDS" ${ARGN})
idf_build_get_property(build_dir BUILD_DIR)
idf_build_get_property(idf_path IDF_PATH)
get_filename_component(embed_file "${embed_file}" ABSOLUTE)
get_filename_component(name "${embed_file}" NAME)
set(embed_srcfile "${build_dir}/${name}.S")
set(rename_to_arg)
if(__RENAME_TO) # use a predefined variable name
set(rename_to_arg -D "VARIABLE_BASENAME=${__RENAME_TO}")
endif()
add_custom_command(OUTPUT "${embed_srcfile}"
COMMAND "${CMAKE_COMMAND}"
-D "DATA_FILE=${embed_file}"
-D "SOURCE_FILE=${embed_srcfile}"
${rename_to_arg}
-D "FILE_TYPE=${embed_type}"
-P "${idf_path}/tools/cmake/scripts/data_file_embed_asm.cmake"
MAIN_DEPENDENCY "${embed_file}"
DEPENDS "${idf_path}/tools/cmake/scripts/data_file_embed_asm.cmake" ${__DEPENDS}
WORKING_DIRECTORY "${build_dir}"
VERBATIM)
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY ADDITIONAL_CLEAN_FILES "${embed_srcfile}")
target_sources("${target}" PRIVATE "${embed_srcfile}")
endfunction()