ldgen.cmake 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Utilities for supporting linker script generation in the build system
  2. # ldgen_create_target
  3. #
  4. # Create the custom target to attach the fragment files and template files
  5. # for the build to.
  6. function(ldgen_set_variables)
  7. add_custom_target(ldgen_section_infos)
  8. add_custom_target(ldgen DEPENDS ldgen_section_infos)
  9. endfunction()
  10. # ldgen_add_fragment_file
  11. #
  12. # Add one or more linker fragment files, and append it to the list of fragment
  13. # files found so far.
  14. function(ldgen_add_fragment_files target fragment_files)
  15. spaces2list(fragment_files)
  16. foreach(fragment_file ${fragment_files})
  17. get_filename_component(fragment_file_abs_dir ${fragment_file} ABSOLUTE BASE_DIR ${component_dir})
  18. list(APPEND fragment_files_full_path ${fragment_file_abs_dir})
  19. endforeach()
  20. set_property(TARGET ldgen APPEND PROPERTY FRAGMENT_FILES ${fragment_files_full_path})
  21. endfunction()
  22. # ldgen_generate_sections_info
  23. #
  24. # Generate sections info for specified target to be used in linker script generation
  25. function(ldgen_generate_sections_info target)
  26. get_filename_component(target_sections_info ${CMAKE_CURRENT_BINARY_DIR}/${target}.sections_info ABSOLUTE)
  27. add_custom_command(
  28. OUTPUT ${target_sections_info}
  29. COMMAND ${CMAKE_OBJDUMP} $<TARGET_FILE:${target}> -h > ${target_sections_info}
  30. DEPENDS ${target}
  31. )
  32. add_custom_target(${target}_sections_info DEPENDS ${target_sections_info})
  33. add_dependencies(ldgen_section_infos ${target}_sections_info)
  34. set_property(TARGET ldgen_section_infos APPEND PROPERTY SECTIONS_INFO_FILES ${target_sections_info})
  35. endfunction()
  36. # ldgen_process_template
  37. #
  38. # Passes a linker script template to the linker script generation tool for
  39. # processing
  40. function(ldgen_process_template template output)
  41. file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/ldgen.section_infos
  42. CONTENT "$<JOIN:$<TARGET_PROPERTY:ldgen_section_infos,SECTIONS_INFO_FILES>,\n>")
  43. # Create command to invoke the linker script generator tool.
  44. add_custom_command(
  45. OUTPUT ${output}
  46. COMMAND ${PYTHON} ${IDF_PATH}/tools/ldgen/ldgen.py
  47. --config ${SDKCONFIG}
  48. --fragments "$<JOIN:$<TARGET_PROPERTY:ldgen,FRAGMENT_FILES>,\t>"
  49. --input ${template}
  50. --output ${output}
  51. --sections ${CMAKE_BINARY_DIR}/ldgen.section_infos
  52. --kconfig ${IDF_PATH}/Kconfig
  53. --env "COMPONENT_KCONFIGS=${COMPONENT_KCONFIGS}"
  54. --env "COMPONENT_KCONFIGS_PROJBUILD=${COMPONENT_KCONFIGS_PROJBUILD}"
  55. --env "IDF_CMAKE=y"
  56. --env "IDF_PATH=${IDF_PATH}"
  57. --env "IDF_TARGET=${IDF_TARGET}"
  58. DEPENDS ${template} $<TARGET_PROPERTY:ldgen,FRAGMENT_FILES> ${SDKCONFIG}
  59. ldgen_section_infos
  60. )
  61. get_filename_component(output_name ${output} NAME)
  62. add_custom_target(ldgen_${output_name}_script DEPENDS ${output})
  63. add_dependencies(ldgen ldgen_${output_name}_script)
  64. endfunction()
  65. # ldgen_create_commands
  66. #
  67. # Create the command to generate the output scripts from templates presented.
  68. function(ldgen_add_dependencies)
  69. if(IDF_PROJECT_EXECUTABLE)
  70. add_dependencies(${IDF_PROJECT_EXECUTABLE} ldgen)
  71. endif()
  72. endfunction()