mirror of
https://github.com/espressif/esp-idf.git
synced 2026-04-28 03:23:14 +00:00
fix(cmakev2/utilities): add a dependency target for the embedded file
Currently, embedded files can be added in two ways. First, by calling the `target_add_binary_data` function directly within a component. Second, by passing `EMBED_FILES` or `EMBED_TXTFILES` to `idf_component_register`, or in cmakev2 by setting the `EMBED_FILES` or `EMBED_TXTFILES` component properties. The source file for an embedded file is generated using `add_custom_command`. When the embedded file is added directly in the component's CMakeLists.txt file by using the `target_add_binary_data` function, the `add_custom_command` command is evaluated in the same directory context where the component target is created. As a result, the generated embedded file can be used automatically as a file dependency in component's sources. However, when an embedded file is added via `idf_component_register` or by setting the component property, the call to `add_custom_command` inside `target_add_binary_data` occurs after the component has been evaluated, specifically after the `add_subdirectory` call, within `idf_component_include`. This means it is not created in the same directory context as the component's target. In this case, the embedded file dependency is not added to the component's target, and the embedded file is not generated. This behavior is described in the `add_custom_command` documentation [1]. > A target created in the same directory (CMakeLists.txt file) > that specifies any output of the custom command as a source > file is given a rule to generate the file using the command at > build time. To fix this issue, an explicit custom target for the generated embedded file is created and added as a dependency of the component's target, ensuring that the file is generated correctly. [1] - https://cmake.org/cmake/help/latest/command/add_custom_command.html Signed-off-by: Frantisek Hrbata <frantisek.hrbata@espressif.com>
This commit is contained in:
@@ -819,7 +819,12 @@ function(target_add_binary_data target embed_file embed_type)
|
||||
idf_build_get_property(build_dir BUILD_DIR)
|
||||
idf_build_get_property(idf_path IDF_PATH)
|
||||
|
||||
get_filename_component(embed_file "${embed_file}" ABSOLUTE)
|
||||
# The target_add_binary_data function is also called within the
|
||||
# idf_component_include function, which is not executed in the component
|
||||
# directory context. Therefore, ensure that the absolute path of the
|
||||
# embedded file is resolved relative to the component directory.
|
||||
idf_component_get_property(component_directory "${target}" COMPONENT_DIR)
|
||||
get_filename_component(embed_file "${embed_file}" ABSOLUTE BASE_DIR "${component_directory}")
|
||||
|
||||
get_filename_component(name "${embed_file}" NAME)
|
||||
set(embed_srcfile "${build_dir}/${name}.S")
|
||||
@@ -841,6 +846,22 @@ function(target_add_binary_data target embed_file embed_type)
|
||||
WORKING_DIRECTORY "${build_dir}"
|
||||
VERBATIM)
|
||||
|
||||
# A file generated by `add_custom_command` can be used as a dependency only
|
||||
# within the directory context where the target was created (see
|
||||
# https://cmake.org/cmake/help/latest/command/add_custom_command.html).
|
||||
# Since embedded files are collected in the EMBED_FILES and EMBED_TXTFILES
|
||||
# component properties, and the source files are generated in the
|
||||
# idf_component_include function, which is not called in the component’s
|
||||
# directory context, we must create an explicit target and add it as a
|
||||
# dependency of the component target. To avoid potential target name
|
||||
# collisions with embedded files that share the same name but reside in
|
||||
# different directories, add a hash of the full embedded file path to
|
||||
# the generated target name.
|
||||
string(MD5 hash "${embed_file}")
|
||||
string(MAKE_C_IDENTIFIER "gen_${name}_${hash}" embed_srcfile_target)
|
||||
add_custom_target(${embed_srcfile_target} DEPENDS "${embed_srcfile}")
|
||||
add_dependencies(${target} ${embed_srcfile_target})
|
||||
|
||||
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY ADDITIONAL_CLEAN_FILES "${embed_srcfile}")
|
||||
|
||||
target_sources("${target}" PRIVATE "${embed_srcfile}")
|
||||
|
||||
Reference in New Issue
Block a user