run_serial_tool.cmake 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # A CMake script to run serial tool commands supporting ESPPORT and
  2. # ESPBAUD environment variables from within ninja or make or another
  3. # cmake-based build runner.
  4. #
  5. # It is recommended to NOT USE this CMake script if you have the option of
  6. # running the tool directly. This script exists only for use inside CMake builds.
  7. cmake_minimum_required(VERSION 3.16)
  8. if(NOT IDF_PATH)
  9. message(FATAL_ERROR "IDF_PATH not set.")
  10. endif()
  11. if(NOT SERIAL_TOOL OR NOT SERIAL_TOOL_ARGS)
  12. message(FATAL_ERROR "SERIAL_TOOL and SERIAL_TOOL_ARGS must "
  13. "be specified on the CMake command line. For direct execution, it is "
  14. "strongly recommended to run ${SERIAL_TOOL} directly.")
  15. endif()
  16. # Propagate the IDF_ENV_FPGA to esptool, for Espressif internal use only
  17. if(DEFINED ENV{IDF_ENV_FPGA})
  18. set(ENV{ESPTOOL_ENV_FPGA} 1)
  19. message("Note: IDF_ENV_FPGA is set, propagating to esptool with ESPTOOL_ENV_FPGA = 1")
  20. endif()
  21. set(serial_tool_cmd ${SERIAL_TOOL})
  22. # Main purpose of this script: we can't expand these environment variables in the main IDF CMake build,
  23. # because we want to expand them at flashing time not at CMake runtime (so they can change
  24. # without needing a CMake re-run)
  25. set(ESPPORT $ENV{ESPPORT})
  26. if(NOT ESPPORT)
  27. message("Note: ${SERIAL_TOOL} will search for a serial port. "
  28. "To specify a port, set the ESPPORT environment variable.")
  29. else()
  30. list(APPEND serial_tool_cmd -p ${ESPPORT})
  31. endif()
  32. set(ESPBAUD $ENV{ESPBAUD})
  33. if(NOT ESPBAUD)
  34. message("Note: ${SERIAL_TOOL} will attempt to set baud rate automatically. "
  35. "To specify a baud rate, set the ESPBAUD environment variable.")
  36. else()
  37. list(APPEND serial_tool_cmd -b ${ESPBAUD})
  38. endif()
  39. # SERIAL_TOOL_ARGS is defined during the first cmake run
  40. # SERIAL_TOOL_EXTRA_ARGS is used for additional arguments from the command line during run-time
  41. list(APPEND serial_tool_cmd ${SERIAL_TOOL_ARGS})
  42. list(APPEND serial_tool_cmd $ENV{SERIAL_TOOL_EXTRA_ARGS})
  43. if(${SERIAL_TOOL_SILENT})
  44. execute_process(COMMAND ${serial_tool_cmd}
  45. WORKING_DIRECTORY "${WORKING_DIRECTORY}"
  46. RESULT_VARIABLE result
  47. OUTPUT_VARIABLE SERIAL_TOOL_OUTPUT_LOG
  48. )
  49. else()
  50. execute_process(COMMAND ${serial_tool_cmd}
  51. WORKING_DIRECTORY "${WORKING_DIRECTORY}"
  52. RESULT_VARIABLE result
  53. )
  54. endif()
  55. if(${result})
  56. # No way to have CMake silently fail, unfortunately
  57. message(FATAL_ERROR "${SERIAL_TOOL} failed. \n${SERIAL_TOOL_OUTPUT_LOG}")
  58. endif()