run_serial_tool.cmake 1.8 KB

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