CMakeLists.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. cmake_minimum_required(VERSION 3.5)
  2. include(${IDF_PATH}/tools/cmake/utilities.cmake)
  3. project(${ULP_APP_NAME} ASM C)
  4. set(version_pattern "[a-z0-9\.-]+")
  5. # Check assembler version
  6. execute_process(
  7. COMMAND ${CMAKE_ASM_COMPILER} --version
  8. OUTPUT_VARIABLE as_output
  9. ERROR_QUIET)
  10. string(REGEX MATCH "\\(GNU Binutils\\) (${version_pattern})" as_version ${as_output})
  11. set(as_version ${CMAKE_MATCH_1})
  12. message(STATUS "Building ULP app ${ULP_APP_NAME}")
  13. message(STATUS "ULP assembler version: ${as_version}")
  14. # Check the supported assembler version
  15. file(STRINGS ${IDF_PATH}/components/ulp/toolchain_ulp_version.mk version_file_contents)
  16. string(REGEX MATCH "SUPPORTED_ULP_ASSEMBLER_VERSION = (${version_pattern})" as_supported_version ${version_file_contents})
  17. set(as_supported_version ${CMAKE_MATCH_1})
  18. if(NOT as_version STREQUAL as_supported_version)
  19. message(WARNING "WARNING: ULP assembler version ${as_version} is not supported. Expected to see version: \
  20. ${as_supported_version}. Please check ESP-IDF ULP setup instructions and update \
  21. the toolchain, or proceed at your own risk.")
  22. endif()
  23. set(ULP_MAP_GEN ${PYTHON} ${IDF_PATH}/components/ulp/esp32ulp_mapgen.py)
  24. set(ULP_LD_TEMPLATE ${IDF_PATH}/components/ulp/ld/esp32.ulp.ld)
  25. get_filename_component(sdkconfig_dir ${SDKCONFIG} DIRECTORY)
  26. foreach(include ${COMPONENT_INCLUDES})
  27. list(APPEND component_includes -I${include})
  28. endforeach()
  29. list(APPEND ULP_PREPROCESSOR_ARGS ${component_includes})
  30. list(APPEND ULP_PREPROCESSOR_ARGS -I${COMPONENT_DIR})
  31. list(APPEND ULP_PREPROCESSOR_ARGS -I${sdkconfig_dir})
  32. include_directories(${component_includes})
  33. list(APPEND ULP_PREPROCESSOR_ARGS -D__ASSEMBLER__)
  34. # Preprocess linker script, pre-linking
  35. get_filename_component(ULP_LD_SCRIPT ${ULP_LD_TEMPLATE} NAME)
  36. add_custom_command( OUTPUT ${ULP_LD_SCRIPT}
  37. COMMAND ${CMAKE_C_COMPILER} -E -P -xc -o ${ULP_LD_SCRIPT} ${ULP_PREPROCESSOR_ARGS} ${ULP_LD_TEMPLATE}
  38. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  39. DEPENDS ${ULP_LD_TEMPLATE}
  40. VERBATIM)
  41. add_custom_target(${ULP_APP_NAME}_ld_script
  42. DEPENDS ${ULP_LD_SCRIPT}
  43. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  44. foreach(ulp_s_source ${ULP_S_SOURCES})
  45. get_filename_component(ulp_ps_source ${ulp_s_source} NAME_WE)
  46. set(ulp_ps_output ${CMAKE_CURRENT_BINARY_DIR}/${ulp_ps_source}.ulp.S)
  47. # Generate preprocessed assembly files.
  48. add_custom_command( OUTPUT ${ulp_ps_output}
  49. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  50. COMMAND ${CMAKE_C_COMPILER} -E -P -xc ${ULP_PREPROCESSOR_ARGS} -o ${ulp_ps_output} ${ulp_s_source}
  51. DEPENDS ${ulp_s_source} ${ULP_LD_SCRIPT}
  52. VERBATIM)
  53. # During assembly file compilation, output listing files as well.
  54. set_source_files_properties(${ulp_ps_output}
  55. PROPERTIES COMPILE_FLAGS
  56. "-al=${CMAKE_CURRENT_BINARY_DIR}/${ulp_ps_source}.lst")
  57. list(APPEND ULP_PS_SOURCES ${ulp_ps_output})
  58. endforeach()
  59. # Create an executable
  60. add_executable(${ULP_APP_NAME} ${ULP_PS_SOURCES})
  61. # Dump the list of global symbols in a convenient format
  62. add_custom_command( OUTPUT ${ULP_APP_NAME}.sym
  63. COMMAND ${CMAKE_NM} -g -f posix $<TARGET_FILE:${ULP_APP_NAME}> > ${ULP_APP_NAME}.sym
  64. DEPENDS ${ULP_APP_NAME}
  65. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  66. # Dump the binary for inclusion into the project
  67. add_custom_command( OUTPUT ${ULP_APP_NAME}.bin
  68. COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${ULP_APP_NAME}> ${ULP_APP_NAME}.bin
  69. DEPENDS ${ULP_APP_NAME}
  70. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  71. add_custom_command( OUTPUT ${ULP_APP_NAME}.ld ${ULP_APP_NAME}.h
  72. COMMAND ${ULP_MAP_GEN} -s ${ULP_APP_NAME}.sym -o ${ULP_APP_NAME}
  73. DEPENDS ${ULP_APP_NAME}.sym
  74. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  75. # Building the component separately from the project should result in
  76. # ULP files being built.
  77. add_custom_target(build
  78. DEPENDS ${ULP_APP_NAME} ${ULP_APP_NAME}.bin ${ULP_APP_NAME}.sym
  79. ${CMAKE_CURRENT_BINARY_DIR}/${ULP_APP_NAME}.ld
  80. ${CMAKE_CURRENT_BINARY_DIR}/${ULP_APP_NAME}.h
  81. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  82. target_link_libraries(${ULP_APP_NAME} -T${CMAKE_CURRENT_BINARY_DIR}/${ULP_LD_SCRIPT} -Map=${CMAKE_CURRENT_BINARY_DIR}/${ULP_APP_NAME}.map)