|
| 1 | +# A CMake script to run esptool commands from within ninja or make |
| 2 | +# or another cmake-based build runner |
| 3 | +# |
| 4 | +# (Needed to expand environment variables, for backwards compatibility.) |
| 5 | +# |
| 6 | +# It is recommended to NOT USE this CMake script if you have the option of |
| 7 | +# running esptool.py directly. This script exists only for use inside CMake builds. |
| 8 | +# |
| 9 | +cmake_minimum_required(VERSION 3.5) |
| 10 | + |
| 11 | +if(NOT IDF_PATH OR NOT ESPTOOLPY OR NOT ESPTOOL_ARGS OR NOT ESPTOOL_WORKING_DIR) |
| 12 | + message(FATAL_ERROR "IDF_PATH, ESPTOOLPY, ESPTOOL_ARGS, and ESPTOOL_WORKING_DIR must " |
| 13 | + "be specified on the CMake command line. For direct esptool execution, it is " |
| 14 | + "strongly recommended to run esptool.py directly.") |
| 15 | +endif() |
| 16 | + |
| 17 | +# Note: we can't expand these environment variables in the main IDF CMake build, |
| 18 | +# because we want to expand them at flashing time not at CMake runtime (so they can change |
| 19 | +# without needing a CMake re-run) |
| 20 | +set(ESPPORT $ENV{ESPPORT}) |
| 21 | +if(NOT ESPPORT) |
| 22 | + message("Note: esptool.py will search for a serial port. To specify a port, set the ESPPORT environment variable.") |
| 23 | +else() |
| 24 | + set(port_arg "-p ${ESPPORT}") |
| 25 | +endif() |
| 26 | + |
| 27 | +set(ESPBAUD $ENV{ESPBAUD}) |
| 28 | +if(NOT ESPBAUD) |
| 29 | + message("Note: Using default baud rate 460800. To modify, set ESPBAUD environment variable.") |
| 30 | + set(ESPBAUD 460800) |
| 31 | +endif() |
| 32 | + |
| 33 | +include("${IDF_PATH}/tools/cmake/utilities.cmake") |
| 34 | + |
| 35 | +set(cmd "${ESPTOOLPY} ${port_arg} -b ${ESPBAUD} ${ESPTOOL_ARGS}") |
| 36 | +spaces2list(cmd) |
| 37 | + |
| 38 | +execute_process(COMMAND ${cmd} |
| 39 | + WORKING_DIRECTORY "${ESPTOOL_WORKING_DIR}" |
| 40 | + RESULT_VARIABLE result |
| 41 | + ) |
| 42 | + |
| 43 | +if(${result}) |
| 44 | + # No way to have CMake silently fail, unfortunately |
| 45 | + message(FATAL_ERROR "esptool.py failed") |
| 46 | +endif() |
0 commit comments