run_serial_tool.cmake 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. list(APPEND serial_tool_cmd ${SERIAL_TOOL_ARGS})
  40. execute_process(COMMAND ${serial_tool_cmd}
  41. WORKING_DIRECTORY "${WORKING_DIRECTORY}"
  42. RESULT_VARIABLE result
  43. )
  44. if(${result})
  45. # No way to have CMake silently fail, unfortunately
  46. message(FATAL_ERROR "${SERIAL_TOOL} failed")
  47. endif()