1# spiffs_create_partition_image
2#
3# Create a spiffs image of the specified directory on the host during build and optionally
4# have the created image flashed using `idf.py flash`
5function(spiffs_create_partition_image partition base_dir)
6    set(options FLASH_IN_PROJECT)
7    set(multi DEPENDS)
8    cmake_parse_arguments(arg "${options}" "" "${multi}" "${ARGN}")
9
10    idf_build_get_property(idf_path IDF_PATH)
11    set(spiffsgen_py ${PYTHON} ${idf_path}/components/spiffs/spiffsgen.py)
12
13    get_filename_component(base_dir_full_path ${base_dir} ABSOLUTE)
14
15    partition_table_get_partition_info(size "--partition-name ${partition}" "size")
16    partition_table_get_partition_info(offset "--partition-name ${partition}" "offset")
17
18    if("${size}" AND "${offset}")
19        set(image_file ${CMAKE_BINARY_DIR}/${partition}.bin)
20
21        if(CONFIG_SPIFFS_USE_MAGIC)
22            set(use_magic "--use-magic")
23        endif()
24
25        if(CONFIG_SPIFFS_USE_MAGIC_LENGTH)
26            set(use_magic_len "--use-magic-len")
27        endif()
28
29        if(CONFIG_SPIFFS_FOLLOW_SYMLINKS)
30            set(follow_symlinks "--follow-symlinks")
31        endif()
32
33        # Execute SPIFFS image generation; this always executes as there is no way to specify for CMake to watch for
34        # contents of the base dir changing.
35        add_custom_target(spiffs_${partition}_bin ALL
36            COMMAND ${spiffsgen_py} ${size} ${base_dir_full_path} ${image_file}
37            --page-size=${CONFIG_SPIFFS_PAGE_SIZE}
38            --obj-name-len=${CONFIG_SPIFFS_OBJ_NAME_LEN}
39            --meta-len=${CONFIG_SPIFFS_META_LENGTH}
40            ${follow_symlinks}
41            ${use_magic}
42            ${use_magic_len}
43            DEPENDS ${arg_DEPENDS}
44            )
45
46        set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
47            ADDITIONAL_MAKE_CLEAN_FILES
48            ${image_file})
49
50        idf_component_get_property(main_args esptool_py FLASH_ARGS)
51        idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS)
52        # Last (optional) parameter is the encryption for the target. In our
53        # case, spiffs is not encrypt so pass FALSE to the function.
54        esptool_py_flash_target(${partition}-flash "${main_args}" "${sub_args}" ALWAYS_PLAINTEXT)
55        esptool_py_flash_to_partition(${partition}-flash "${partition}" "${image_file}")
56
57        add_dependencies(${partition}-flash spiffs_${partition}_bin)
58
59        if(arg_FLASH_IN_PROJECT)
60            esptool_py_flash_to_partition(flash "${partition}" "${image_file}")
61            add_dependencies(flash spiffs_${partition}_bin)
62        endif()
63    else()
64        set(message "Failed to create SPIFFS image for partition '${partition}'. "
65                    "Check project configuration if using the correct partition table file.")
66        fail_at_build_time(spiffs_${partition}_bin "${message}")
67    endif()
68endfunction()
69