squeezelite.cmake 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Check if the required dependencies are installed
  2. find_package(Python3 COMPONENTS Interpreter)
  3. if(Python3_Interpreter_FOUND)
  4. execute_process(COMMAND pip3 install protobuf grpcio-tools)
  5. else()
  6. message(FATAL_ERROR "Python3 interpreter not found. Please install Python3 before building the project.")
  7. endif()
  8. include($ENV{IDF_PATH}/tools/cmake/project.cmake)
  9. function(___register_flash partition_name sub_type)
  10. message(STATUS "Adding new build target (from build folder): ninja ${partition_name}-flash")
  11. partition_table_get_partition_info(otaapp_offset "--partition-type app --partition-subtype ${sub_type}" "offset")
  12. idf_component_get_property(main_args esptool_py FLASH_ARGS)
  13. idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS)
  14. esptool_py_flash_target(${partition_name}-flash "${main_args}" "${sub_args}")
  15. esptool_py_flash_target_image(${partition_name}-flash ${partition_name} "${otaapp_offset}" "${build_dir}/${partition_name}.bin")
  16. esptool_py_flash_target_image(flash ${partition_name} "${otaapp_offset}" "${build_dir}/${partition_name}.bin")
  17. endfunction()
  18. #
  19. # Removes the specified compile flag from the specified target.
  20. # _target - The target to remove the compile flag from
  21. # _flag - The compile flag to remove
  22. #
  23. # Pre: apply_global_cxx_flags_to_all_targets() must be invoked.
  24. #
  25. macro(remove_flag_from_target _target _flag)
  26. get_target_property(_target_cxx_flags ${_target} COMPILE_OPTIONS)
  27. if(_target_cxx_flags)
  28. list(REMOVE_ITEM _target_cxx_flags ${_flag})
  29. set_target_properties(${_target} PROPERTIES COMPILE_OPTIONS "${_target_cxx_flags}")
  30. endif()
  31. endmacro()
  32. function(___print_list pref listcontent)
  33. message("")
  34. message("${pref}")
  35. foreach(e ${listcontent})
  36. message("${pref} ${e}")
  37. endforeach()
  38. message("")
  39. endfunction()
  40. function(___create_new_target target_name)
  41. idf_build_get_property(build_dir BUILD_DIR)
  42. idf_build_get_property(python PYTHON)
  43. file(TO_CMAKE_PATH "${IDF_PATH}" idf_path)
  44. set(target_elf ${target_name}.elf)
  45. # Create a dummy file to work around CMake requirement of having a source
  46. # file while adding an executable
  47. set(target_elf_src ${CMAKE_BINARY_DIR}/${target_name}_src.c)
  48. add_custom_command(OUTPUT ${target_elf_src}
  49. BUILD
  50. COMMAND ${CMAKE_COMMAND} -E touch ${target_elf_src}
  51. VERBATIM)
  52. add_custom_target(_${target_name}_elf DEPENDS "${target_elf_src}" )
  53. add_executable(${target_elf} "${target_elf_src}")
  54. add_dependencies(${target_elf} _${target_name}_elf)
  55. add_dependencies(${target_elf} "recovery.elf")
  56. set_property(TARGET ${target_elf} PROPERTY RECOVERY_PREFIX app_${target_name})
  57. set(ESPTOOLPY_ELF2IMAGE_OPTIONS --elf-sha256-offset 0xb0)
  58. # Remove app_recovery so that app_squeezelite and dependencies are properly resolved
  59. idf_build_get_property(bca BUILD_COMPONENT_ALIASES)
  60. list(REMOVE_ITEM bca "idf::app_recovery")
  61. list(REMOVE_ITEM bca "idf::app_squeezelite")
  62. target_link_libraries(${target_elf} ${bca})
  63. target_link_libraries(${target_elf} idf::app_squeezelite)
  64. set(target_name_mapfile "${target_name}.map")
  65. target_link_libraries(${target_elf} "-Wl,--cref -Wl,--Map=${CMAKE_BINARY_DIR}/${target_name_mapfile}")
  66. # idf_build_get_property(link_depends __LINK_DEPENDS)
  67. # idf_build_get_property(link_options LINK_OPTIONS)
  68. # idf_build_get_property(ldgen_libraries __LDGEN_LIBRARIES GENERATOR_EXPRESSION)
  69. add_custom_command(
  70. TARGET ${target_elf}
  71. POST_BUILD
  72. COMMAND ${CMAKE_COMMAND} -E echo "Generating ${build_dir}/${target_name}.bin"
  73. COMMAND ${ESPTOOLPY} elf2image ${ESPTOOLPY_FLASH_OPTIONS} ${ESPTOOLPY_ELF2IMAGE_OPTIONS} -o "${build_dir}/${target_name}.bin" "${target_name}.elf"
  74. DEPENDS "${target_name}.elf"
  75. WORKING_DIRECTORY ${build_dir}
  76. COMMENT "Generating binary image from built executable"
  77. VERBATIM
  78. )
  79. set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
  80. ADDITIONAL_MAKE_CLEAN_FILES
  81. "${build_dir}/${target_name_mapfile}" "${build_dir}/${target_elf_src}" )
  82. set(idf_size ${python} ${IDF_PATH}/tools/idf_size.py)
  83. if(DEFINED OUTPUT_JSON AND OUTPUT_JSON)
  84. list(APPEND idf_size "--json")
  85. endif()
  86. # Add size targets, depend on map file, run idf_size.py
  87. message(STATUS "Adding new build target (from build folder): ninja size-${target_name}")
  88. add_custom_target(size-${target_name}
  89. DEPENDS ${target_elf}
  90. COMMAND ${idf_size} ${target_name_mapfile}
  91. )
  92. message(STATUS "Adding new build target (from build folder): ninja size-files-${target_name}")
  93. add_custom_target(size-files-${target_name}
  94. DEPENDS ${target_elf}
  95. COMMAND ${idf_size} --files ${target_name_mapfile}
  96. )
  97. message(STATUS "Adding new build target (from build folder): ninja size-components-${target_name}")
  98. add_custom_target(size-components-${target_name}
  99. DEPENDS ${target_elf}
  100. COMMAND ${idf_size} --archives ${target_name_mapfile}
  101. )
  102. unset(idf_size)
  103. endfunction()
  104. ___create_new_target(squeezelite )
  105. ___register_flash(squeezelite ota_0)
  106. add_custom_target(_jtag_scripts ALL
  107. BYPRODUCTS "flash_dbg_project_args"
  108. POST_BUILD
  109. COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/generate_debug_scripts.cmake"
  110. # COMMAND ${CMAKE_COMMAND} --graphviz=graph.dot .
  111. # $ sed -n 's/.*label="\(.*\)"\s.*/\1/p' graph.dot.foo > foo_dependencies.txt
  112. )
  113. add_dependencies(partition_table _jtag_scripts)
  114. idf_build_get_property(build_dir BUILD_DIR)
  115. add_custom_command(
  116. TARGET recovery.elf
  117. PRE_LINK
  118. COMMAND xtensa-esp32-elf-objcopy --weaken-symbol esp_app_desc ${build_dir}/esp-idf/app_update/libapp_update.a
  119. COMMAND xtensa-esp32-elf-objcopy --weaken-symbol register_external ${build_dir}/esp-idf/squeezelite/libsqueezelite.a
  120. COMMAND xtensa-esp32-elf-objcopy --weaken-symbol deregister_external ${build_dir}/esp-idf/squeezelite/libsqueezelite.a
  121. COMMAND xtensa-esp32-elf-objcopy --weaken-symbol decode_restore ${build_dir}/esp-idf/squeezelite/libsqueezelite.a
  122. # COMMAND xtensa-esp32-elf-objcopy --strip-symbol start_ota ${build_dir}/esp-idf/app_squeezelite/libapp_squeezelite.a
  123. ## IDF-V4.2+ COMMAND xtensa-esp32-elf-objcopy --weaken-symbol main ${build_dir}/esp-idf/squeezelite/libsqueezelite.a
  124. COMMAND xtensa-esp32-elf-objcopy --globalize-symbol find_command_by_name ${build_dir}/esp-idf/console/libconsole.a
  125. VERBATIM
  126. )
  127. add_custom_command(
  128. TARGET squeezelite.elf
  129. PRE_LINK
  130. # COMMAND xtensa-esp32-elf-objcopy --strip-symbol start_ota ${build_dir}/esp-idf/app_recovery/libapp_recovery.a
  131. COMMAND xtensa-esp32-elf-objcopy --weaken-symbol esp_app_desc ${build_dir}/esp-idf/app_update/libapp_update.a
  132. COMMAND xtensa-esp32-elf-objcopy --globalize-symbol find_command_by_name ${build_dir}/esp-idf/console/libconsole.a
  133. ## IDF-V4.2+ COMMAND xtensa-esp32-elf-objcopy --weaken-symbol main ${build_dir}/esp-idf/app_recovery/libapp_recovery.a
  134. VERBATIM
  135. )