CMakeLists.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # SPDX-License-Identifier: Apache-2.0
  2. function(runners_yaml_append content)
  3. # Append ${content}\n to a target property which is later evaluated as a
  4. # generator expression when writing the flash runner yaml file.
  5. # We define this function here to have access to the `flash` target.
  6. set_property(
  7. TARGET runners_yaml_props_target
  8. APPEND_STRING
  9. PROPERTY yaml_contents
  10. "${content}\n"
  11. )
  12. endfunction()
  13. function(get_runners_prop prop out_var default_value)
  14. # Get property 'prop' from runners_yaml_props_target, storing its
  15. # value in 'out_var'. If the property is not found (value is
  16. # ...-NOTFOUND), 'out_var' is set to 'default_value'.
  17. get_target_property(out runners_yaml_props_target "${prop}")
  18. if("${out}" STREQUAL "out-NOTFOUND")
  19. set("${out_var}" "${default_value}" PARENT_SCOPE)
  20. else()
  21. set("${out_var}" "${out}" PARENT_SCOPE)
  22. endif()
  23. endfunction()
  24. function(runners_yaml_append_config)
  25. # Append the common configuration values to the relevant property target.
  26. runners_yaml_append("\n# Common runner configuration values.")
  27. runners_yaml_append("config:")
  28. runners_yaml_append(" board_dir: ${BOARD_DIR}")
  29. get_runners_prop(elf_file elf "${KERNEL_ELF_NAME}")
  30. runners_yaml_append(" # Build outputs:")
  31. runners_yaml_append(" elf_file: ${elf}")
  32. if(CONFIG_BUILD_OUTPUT_HEX)
  33. get_runners_prop(hex_file hex "${KERNEL_HEX_NAME}")
  34. runners_yaml_append(" hex_file: ${hex}")
  35. endif()
  36. if(CONFIG_BUILD_OUTPUT_BIN)
  37. get_runners_prop(bin_file bin "${KERNEL_BIN_NAME}")
  38. runners_yaml_append(" bin_file: ${bin}")
  39. endif()
  40. if(CMAKE_GDB OR OPENOCD OR OPENOCD_DEFAULT_PATH)
  41. runners_yaml_append(" # Host tools:")
  42. endif()
  43. if(CMAKE_GDB)
  44. runners_yaml_append(" gdb: ${CMAKE_GDB}")
  45. endif()
  46. if(OPENOCD)
  47. runners_yaml_append(" openocd: ${OPENOCD}")
  48. runners_yaml_append(" openocd_search:")
  49. if(OPENOCD_DEFAULT_PATH)
  50. runners_yaml_append(" - ${OPENOCD_DEFAULT_PATH}")
  51. endif()
  52. endif()
  53. runners_yaml_append("")
  54. endfunction()
  55. # Save runner state in a YAML file, and put that YAML file's location
  56. # in the cache.
  57. function(create_runners_yaml)
  58. set(runners ${ARGV})
  59. set(runners_yaml "${PROJECT_BINARY_DIR}/runners.yaml")
  60. runners_yaml_append("# Available runners configured by board.cmake.\nrunners:")
  61. foreach(runner ${runners})
  62. runners_yaml_append("- ${runner}")
  63. endforeach()
  64. if(DEFINED BOARD_FLASH_RUNNER)
  65. runners_yaml_append("\n# Default flash runner if --runner is not given.")
  66. runners_yaml_append("flash-runner: ${BOARD_FLASH_RUNNER}")
  67. endif()
  68. if(DEFINED BOARD_DEBUG_RUNNER)
  69. runners_yaml_append("\n# Default debug runner if --runner is not given.")
  70. runners_yaml_append("debug-runner: ${BOARD_DEBUG_RUNNER}")
  71. endif()
  72. # Sets up common runner configuration values.
  73. runners_yaml_append_config()
  74. # Get runner-specific arguments set in the board files.
  75. runners_yaml_append("# Runner specific arguments")
  76. runners_yaml_append("args:")
  77. foreach(runner ${runners})
  78. string(MAKE_C_IDENTIFIER ${runner} runner_id)
  79. runners_yaml_append(" ${runner}:")
  80. get_property(args GLOBAL PROPERTY "BOARD_RUNNER_ARGS_${runner_id}")
  81. if(args)
  82. # Usually, the runner has arguments. Append them to runners.yaml,
  83. # one per line.
  84. foreach(arg ${args})
  85. runners_yaml_append(" - ${arg}")
  86. endforeach()
  87. else()
  88. # If the runner doesn't need any arguments, just use an empty list.
  89. runners_yaml_append(" []\n")
  90. endif()
  91. endforeach()
  92. # Write the final contents and set its location in the cache.
  93. file(GENERATE OUTPUT "${runners_yaml}" CONTENT
  94. $<TARGET_PROPERTY:runners_yaml_props_target,yaml_contents>)
  95. set(ZEPHYR_RUNNERS_YAML "${runners_yaml}" CACHE INTERNAL
  96. "a configuration file for the runners Python package")
  97. endfunction()
  98. get_property(RUNNERS GLOBAL PROPERTY ZEPHYR_RUNNERS)
  99. # Persist the runner-related state.
  100. #
  101. # Available runners and their arguments are configured in board.cmake
  102. # files.
  103. #
  104. # Everything is marked with FORCE so that re-running CMake updates the
  105. # configuration if the board files change.
  106. if(RUNNERS)
  107. create_runners_yaml(${RUNNERS})
  108. endif()
  109. if(DEFINED ENV{WEST_DIR} AND NOT WEST_DIR)
  110. set(WEST_DIR $ENV{WEST_DIR})
  111. endif(DEFINED ENV{WEST_DIR} AND NOT WEST_DIR)
  112. if(WEST_DIR)
  113. set(WEST "PYTHONPATH=${WEST_DIR}/src" "${PYTHON_EXECUTABLE};${WEST_DIR}/src/west/app/main.py;--zephyr-base=${ZEPHYR_BASE} ")
  114. endif()
  115. # Generate the flash, debug, debugserver, attach targets within the build
  116. # system itself.
  117. foreach(target flash debug debugserver attach)
  118. if(target STREQUAL flash)
  119. set(comment "Flashing ${BOARD}")
  120. elseif(target STREQUAL debug)
  121. set(comment "Debugging ${BOARD}")
  122. elseif(target STREQUAL debugserver)
  123. set(comment "Debugging ${BOARD}")
  124. if(EMU_PLATFORM)
  125. # cmake/qemu/CMakeLists.txt will add a debugserver target for
  126. # emulation platforms, so we don't add one here
  127. continue()
  128. endif()
  129. elseif(target STREQUAL attach)
  130. set(comment "Debugging ${BOARD}")
  131. endif()
  132. string(TOUPPER ${target} TARGET_UPPER)
  133. list(APPEND RUNNERS_DEPS ${logical_target_for_zephyr_elf})
  134. # Enable verbose output, if requested.
  135. if(CMAKE_VERBOSE_MAKEFILE)
  136. set(RUNNER_VERBOSE "--verbose")
  137. else()
  138. set(RUNNER_VERBOSE)
  139. endif()
  140. if(WEST)
  141. add_custom_target(${target}
  142. # This script will print an error message and fail if <target> has added
  143. # dependencies. This is done using dedicated CMake script, as
  144. # `cmake -E {true|false}` is not available until CMake 3.16.
  145. COMMAND ${CMAKE_COMMAND}
  146. -DTARGET=${target}
  147. -DDEPENDENCIES="$<TARGET_PROPERTY:${target},MANUALLY_ADDED_DEPENDENCIES>"
  148. -P ${CMAKE_CURRENT_LIST_DIR}/check_runner_dependencies.cmake
  149. COMMAND
  150. ${CMAKE_COMMAND} -E env
  151. ${WEST}
  152. ${RUNNER_VERBOSE}
  153. ${target}
  154. WORKING_DIRECTORY
  155. ${APPLICATION_BINARY_DIR}
  156. COMMENT
  157. ${comment}
  158. USES_TERMINAL
  159. )
  160. else()
  161. add_custom_target(${target}
  162. COMMAND ${CMAKE_COMMAND} -E echo \"West was not found in path. To support
  163. '${CMAKE_MAKE_PROGRAM} ${target}', please create a west workspace.\"
  164. USES_TERMINAL
  165. )
  166. endif(WEST)
  167. endforeach()