1set(PARTITION_TABLE_OFFSET ${CONFIG_PARTITION_TABLE_OFFSET})
2
3set(PARTITION_TABLE_CHECK_SIZES_TOOL_PATH "${CMAKE_CURRENT_LIST_DIR}/check_sizes.py")
4
5idf_build_get_property(build_dir BUILD_DIR)
6idf_build_set_property(PARTITION_TABLE_BIN_PATH "${build_dir}/partition_table/partition-table.bin")
7
8if(NOT BOOTLOADER_BUILD)
9    # Set PARTITION_CSV_PATH to the configured partition CSV file
10    # absolute path
11    if(CONFIG_PARTITION_TABLE_CUSTOM)
12        idf_build_get_property(project_dir PROJECT_DIR)
13        # Custom filename expands any path relative to the project
14        get_filename_component(PARTITION_CSV_PATH "${CONFIG_PARTITION_TABLE_FILENAME}"
15                                ABSOLUTE BASE_DIR "${project_dir}")
16
17        if(NOT EXISTS "${PARTITION_CSV_PATH}")
18            message(WARNING "Partition table CSV file ${PARTITION_CSV_PATH} not found. "
19                "Change custom partition CSV path in menuconfig.")
20            # Note: partition_table CMakeLists.txt contains some logic to create a dummy
21            # partition_table target in this case, see comments in that file.
22        endif()
23    else()
24        # Other .csv files are always in the component directory
25        get_filename_component(PARTITION_CSV_PATH "${COMPONENT_DIR}/${CONFIG_PARTITION_TABLE_FILENAME}" ABSOLUTE)
26
27        if(NOT EXISTS "${PARTITION_CSV_PATH}")
28            message(FATAL_ERROR "Internal error, built-in ${PARTITION_CSV_PATH} not found.")
29        endif()
30    endif()
31
32    # need to re-run CMake if the partition CSV changes, as the offsets/sizes of partitions may change
33    set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PARTITION_CSV_PATH})
34endif()
35
36# partition_table_get_partition_info
37#
38# Get information about a partition from the partition table
39function(partition_table_get_partition_info result get_part_info_args part_info)
40    idf_build_get_property(python PYTHON)
41    idf_build_get_property(idf_path IDF_PATH)
42    separate_arguments(get_part_info_args)
43    execute_process(COMMAND ${python}
44        ${idf_path}/components/partition_table/parttool.py -q
45        --partition-table-offset ${PARTITION_TABLE_OFFSET}
46        --partition-table-file ${PARTITION_CSV_PATH}
47        get_partition_info ${get_part_info_args} --info ${part_info}
48        OUTPUT_VARIABLE info
49        RESULT_VARIABLE exit_code
50        OUTPUT_STRIP_TRAILING_WHITESPACE)
51    if(NOT ${exit_code} EQUAL 0 AND NOT ${exit_code} EQUAL 1)
52        # can't fail here as it would prevent the user from running 'menuconfig' again
53        message(WARNING "parttool.py execution failed (${result}), problem with partition CSV file (see above)")
54    endif()
55    set(${result} ${info} PARENT_SCOPE)
56endfunction()
57
58# Add a custom target (see add_custom_target) that checks a given binary built by the build system
59# doesn't overflow any partitions with the given partition type and (optional) sub-type.
60#
61# Adding the target doesn't mean it gets called during the build, use add_dependencies to make another
62# target depend on this one.
63#
64# Arguments:
65# - target name - (first argument) name of the target to create
66# - DEPENDS - dependencies the target should have (i.e. whatever target generates the binary).
67# - BINARY_PATH - path to the target binary file to check
68# - PARTITION_TYPE - partition type to check against
69# - PARTITION_SUBTYPE - (optional) partition subtype to check against
70
71function(partition_table_add_check_size_target target_name)
72    # result binary_path partition_type partition_subtype
73    set(args BINARY_PATH PARTITION_TYPE PARTITION_SUBTYPE)
74    set(multi_args DEPENDS)
75    cmake_parse_arguments(CMD "" "${args}" "${multi_args}" ${ARGN})
76
77    idf_build_get_property(python PYTHON)
78    idf_build_get_property(table_bin PARTITION_TABLE_BIN_PATH)
79    if(CMD_PARTITION_SUBTYPE)
80        set(subtype_arg --subtype ${CMD_PARTITION_SUBTYPE})
81    else()
82        set(subtype_arg)
83    endif()
84    set(command ${python} ${PARTITION_TABLE_CHECK_SIZES_TOOL_PATH}
85        --offset ${PARTITION_TABLE_OFFSET}
86        partition --type ${CMD_PARTITION_TYPE} ${subtype_arg}
87        ${table_bin} ${CMD_BINARY_PATH})
88
89    add_custom_target(${target_name} COMMAND ${command} DEPENDS ${CMD_DEPENDS} partition_table_bin)
90endfunction()
91
92# Add a custom target (see add_custom_target) that checks a bootloader binary
93# built by the build system will not overlap the partition table.
94#
95# Adding the target doesn't mean it gets called during the build, use add_dependencies to make another
96# target depend on this one.
97#
98# Arguments:
99# - target name - (first argument) name of the target to create
100# - DEPENDS - dependencies the new target should have (i.e. whatever target generates the bootloader binary)
101# - BOOTLOADER_BINARY_PATH - path to bootloader binary file
102function(partition_table_add_check_bootloader_size_target target_name)
103    cmake_parse_arguments(CMD "" "BOOTLOADER_BINARY_PATH" "DEPENDS" ${ARGN})
104    idf_build_get_property(python PYTHON)
105
106    set(command ${python} ${PARTITION_TABLE_CHECK_SIZES_TOOL_PATH}
107        --offset ${PARTITION_TABLE_OFFSET}
108        bootloader ${BOOTLOADER_OFFSET} ${CMD_BOOTLOADER_BINARY_PATH})
109    add_custom_target(${target_name} COMMAND ${command} DEPENDS ${CMD_DEPENDS})
110endfunction()
111