relinker.cmake 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # @brief Link designated functions from SRAM to Flash to save SRAM
  2. if(CONFIG_CU_RELINKER_ENABLE)
  3. # project_elf variable is only in project.cmake
  4. if(NOT TARGET customer_sections AND DEFINED project_elf)
  5. message(STATUS "Relinker is enabled.")
  6. if(CONFIG_IDF_TARGET_ESP32C2)
  7. set(target "esp32c2")
  8. else()
  9. message(FATAL_ERROR "Other targets are not supported.")
  10. endif()
  11. if(CONFIG_CU_RELINKER_ENABLE_CUSTOMIZED_CONFIGURATION_FILES)
  12. idf_build_get_property(project_dir PROJECT_DIR)
  13. get_filename_component(cfg_file_path "${CONFIG_CU_RELINKER_CUSTOMIZED_CONFIGURATION_FILES_PATH}"
  14. ABSOLUTE BASE_DIR "${project_dir}")
  15. if(NOT EXISTS "${cfg_file_path}")
  16. message(FATAL_ERROR "Relinker Configuration files path ${cfg_file_path} is not found.")
  17. endif()
  18. else()
  19. set(cfg_file_path ${PROJECT_DIR}/relinker/${target})
  20. if(NOT EXISTS ${cfg_file_path})
  21. set(cfg_file_path ${CMAKE_CURRENT_LIST_DIR}/scripts/relinker/examples/${target})
  22. endif()
  23. endif()
  24. message(STATUS "Relinker configuration files: ${cfg_file_path}")
  25. set(library_file "${cfg_file_path}/library.csv")
  26. set(object_file "${cfg_file_path}/object.csv")
  27. set(function_file "${cfg_file_path}/function.csv")
  28. set(relinker_script "${CMAKE_CURRENT_LIST_DIR}/scripts/relinker/relinker.py")
  29. set(cmake_objdump "${CMAKE_OBJDUMP}")
  30. set(link_path "${CMAKE_BINARY_DIR}/esp-idf/esp_system/ld")
  31. set(link_src_file "${link_path}/sections.ld")
  32. set(link_dst_file "${link_path}/customer_sections.ld")
  33. set(relinker_opts --input ${link_src_file}
  34. --output ${link_dst_file}
  35. --library ${library_file}
  36. --object ${object_file}
  37. --function ${function_file}
  38. --sdkconfig ${sdkconfig}
  39. --objdump ${cmake_objdump})
  40. if(CONFIG_CU_RELINKER_ENABLE_PRINT_ERROR_INFO_WHEN_MISSING_FUNCTION)
  41. list(APPEND relinker_opts --missing_function_info True)
  42. endif()
  43. idf_build_get_property(link_depends __LINK_DEPENDS)
  44. add_custom_command(OUTPUT ${link_dst_file}
  45. COMMAND ${python} -B ${relinker_script}
  46. ${relinker_opts}
  47. COMMAND ${CMAKE_COMMAND} -E copy
  48. ${link_dst_file}
  49. ${link_src_file}
  50. COMMAND ${CMAKE_COMMAND} -E echo
  51. /*relinker*/ >>
  52. ${link_dst_file}
  53. DEPENDS "${link_depends}"
  54. "${library_file}"
  55. "${object_file}"
  56. "${function_file}"
  57. VERBATIM)
  58. add_custom_target(customer_sections DEPENDS ${link_dst_file})
  59. add_dependencies(${project_elf} customer_sections)
  60. endif()
  61. else()
  62. message(STATUS "Relinker isn't enabled.")
  63. endif()