/ components / spiffs / project_include.cmake
project_include.cmake
 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`
 5  function(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          esptool_py_flash_target(${partition}-flash "${main_args}" "${sub_args}")
53          esptool_py_flash_target_image(${partition}-flash "${partition}" "${offset}" "${image_file}")
54  
55          add_dependencies(${partition}-flash spiffs_${partition}_bin)
56  
57          if(arg_FLASH_IN_PROJECT)
58              esptool_py_flash_target_image(flash "${partition}" "${offset}" "${image_file}")
59              add_dependencies(flash spiffs_${partition}_bin)
60          endif()
61      else()
62          set(message "Failed to create SPIFFS image for partition '${partition}'. "
63                      "Check project configuration if using the correct partition table file.")
64          fail_at_build_time(spiffs_${partition}_bin "${message}")
65      endif()
66  endfunction()