run_serial_tool.cmake 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.5)
  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. # Main purpose of this script: we can't expand these environment variables in the main IDF CMake build,
  22. # because we want to expand them at flashing time not at CMake runtime (so they can change
  23. # without needing a CMake re-run)
  24. set(ESPPORT $ENV{ESPPORT})
  25. if(NOT ESPPORT)
  26. message("Note: ${SERIAL_TOOL} will search for a serial port. "
  27. "To specify a port, set the ESPPORT environment variable.")
  28. else()
  29. set(port_arg "-p ${ESPPORT}")
  30. endif()
  31. set(ESPBAUD $ENV{ESPBAUD})
  32. if(NOT ESPBAUD)
  33. message("Note: ${SERIAL_TOOL} will attempt to set baud rate automatically. "
  34. "To specify a baud rate, set the ESPBAUD environment variable.")
  35. else()
  36. set(baud_arg "-b ${ESPBAUD}")
  37. endif()
  38. set(serial_tool_cmd "${SERIAL_TOOL} ${port_arg} ${baud_arg} ${SERIAL_TOOL_ARGS}")
  39. include("${IDF_PATH}/tools/cmake/utilities.cmake")
  40. spaces2list(serial_tool_cmd)
  41. execute_process(COMMAND ${serial_tool_cmd}
  42. WORKING_DIRECTORY "${WORKING_DIRECTORY}"
  43. RESULT_VARIABLE result
  44. )
  45. if(${result})
  46. # No way to have CMake silently fail, unfortunately
  47. message(FATAL_ERROR "${SERIAL_TOOL} failed")
  48. endif()