From ca6c093d1add1eae8389535933ba860880bf351c Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Mon, 12 Oct 2020 21:59:20 +0800 Subject: [PATCH 1/5] esptool_py: make script generic for serial ops --- components/esptool_py/get_port_args.cmake | 4 ++-- components/esptool_py/project_include.cmake | 18 +++++++++--------- components/esptool_py/run_cmd.cmake | 2 +- ...run_esptool.cmake => run_serial_tool.cmake} | 10 ++++------ 4 files changed, 16 insertions(+), 18 deletions(-) rename components/esptool_py/{run_esptool.cmake => run_serial_tool.cmake} (70%) diff --git a/components/esptool_py/get_port_args.cmake b/components/esptool_py/get_port_args.cmake index 867094801c..e70f131046 100644 --- a/components/esptool_py/get_port_args.cmake +++ b/components/esptool_py/get_port_args.cmake @@ -3,14 +3,14 @@ # without needing a CMake re-run) set(ESPPORT $ENV{ESPPORT}) if(NOT ESPPORT) - message("Note: ${TOOL} will search for a serial port. To specify a port, set the ESPPORT environment variable.") + message("Note: ${SERIAL_TOOL} will search for a serial port. To specify a port, set the ESPPORT environment variable.") else() set(port_arg "-p ${ESPPORT}") endif() set(ESPBAUD $ENV{ESPBAUD}) if(NOT ESPBAUD) - message("Note: ${TOOL} will attempt to set baud rate automatically. " + message("Note: ${SERIAL_TOOL} will attempt to set baud rate automatically. " "To specify a baud rate, set the ESPBAUD environment variable.") else() set(baud_arg "-b ${ESPBAUD}") diff --git a/components/esptool_py/project_include.cmake b/components/esptool_py/project_include.cmake index cd746a6cda..038a07c937 100644 --- a/components/esptool_py/project_include.cmake +++ b/components/esptool_py/project_include.cmake @@ -133,9 +133,9 @@ endif() add_custom_target(erase_flash COMMAND ${CMAKE_COMMAND} -D IDF_PATH="${idf_path}" - -D ESPTOOLPY="${ESPTOOLPY}" - -D ESPTOOL_ARGS="erase_flash" - -P run_esptool.cmake + -D SERIAL_TOOL="${ESPTOOLPY}" + -D SERIAL_TOOL_ARGS="erase_flash" + -P run_serial_tool.cmake WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} USES_TERMINAL ) @@ -332,10 +332,10 @@ function(esptool_py_flash_target target_name main_args sub_args) add_custom_target(${target_name} COMMAND ${CMAKE_COMMAND} -D IDF_PATH="${idf_path}" - -D ESPTOOLPY="${ESPTOOLPY}" - -D ESPTOOL_ARGS="${main_args};write_flash;@${target_name}_args" + -D SERIAL_TOOL="${ESPTOOLPY}" + -D SERIAL_TOOL_ARGS="${main_args};write_flash;@${target_name}_args" -D WORKING_DIRECTORY="${build_dir}" - -P ${esptool_py_dir}/run_esptool.cmake + -P ${esptool_py_dir}/run_serial_tool.cmake WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} USES_TERMINAL ) @@ -373,10 +373,10 @@ $,\n>") add_custom_target(encrypted-${target_name} COMMAND ${CMAKE_COMMAND} -D IDF_PATH="${idf_path}" - -D ESPTOOLPY="${ESPTOOLPY}" - -D ESPTOOL_ARGS="${main_args};write_flash;@encrypted_${target_name}_args" + -D SERIAL_TOOL="${ESPTOOLPY}" + -D SERIAL_TOOL_ARGS="${main_args};write_flash;@encrypted_${target_name}_args" -D WORKING_DIRECTORY="${build_dir}" - -P ${esptool_py_dir}/run_esptool.cmake + -P ${esptool_py_dir}/run_serial_tool.cmake WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} USES_TERMINAL ) diff --git a/components/esptool_py/run_cmd.cmake b/components/esptool_py/run_cmd.cmake index 8eefabbc21..a5620a3420 100644 --- a/components/esptool_py/run_cmd.cmake +++ b/components/esptool_py/run_cmd.cmake @@ -11,5 +11,5 @@ execute_process(COMMAND ${CMD} if(${result}) # No way to have CMake silently fail, unfortunately - message(FATAL_ERROR "${TOOL} failed") + message(FATAL_ERROR "${SERIAL_TOOL} failed") endif() diff --git a/components/esptool_py/run_esptool.cmake b/components/esptool_py/run_serial_tool.cmake similarity index 70% rename from components/esptool_py/run_esptool.cmake rename to components/esptool_py/run_serial_tool.cmake index e632457c36..04d3f5afdf 100644 --- a/components/esptool_py/run_esptool.cmake +++ b/components/esptool_py/run_serial_tool.cmake @@ -8,14 +8,12 @@ # cmake_minimum_required(VERSION 3.5) -set(TOOL "esptool.py") - -if(NOT ESPTOOLPY OR NOT ESPTOOL_ARGS) - message(FATAL_ERROR "ESPTOOLPY and ESPTOOL_ARGS must " +if(NOT SERIAL_TOOL OR NOT SERIAL_TOOL_ARGS) + message(FATAL_ERROR "SERIAL_TOOL and SERIAL_TOOL_ARGS must " "be specified on the CMake command line. For direct execution, it is " - "strongly recommended to run ${TOOL} directly.") + "strongly recommended to run ${SERIAL_TOOL} directly.") endif() include("${CMAKE_CURRENT_LIST_DIR}/get_port_args.cmake") -set(CMD "${ESPTOOLPY} ${port_arg} ${baud_arg} ${ESPTOOL_ARGS}") +set(CMD "${SERIAL_TOOL} ${port_arg} ${baud_arg} ${SERIAL_TOOL_ARGS}") include("${CMAKE_CURRENT_LIST_DIR}/run_cmd.cmake") From dec7b86721c9b3c0f2766bc4b0bb78fd0403b775 Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Mon, 12 Oct 2020 22:12:49 +0800 Subject: [PATCH 2/5] app_update: support port and baud env variables for otatool targets Closes https://github.com/espressif/esp-idf/issues/5280 --- components/app_update/CMakeLists.txt | 30 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/components/app_update/CMakeLists.txt b/components/app_update/CMakeLists.txt index f1d6b11697..a8492e351a 100644 --- a/components/app_update/CMakeLists.txt +++ b/components/app_update/CMakeLists.txt @@ -48,19 +48,31 @@ if(NOT BOOTLOADER_BUILD) set(otatool_py ${python} ${COMPONENT_DIR}/otatool.py) - set(esptool_args --esptool-args before=${CONFIG_ESPTOOLPY_BEFORE} after=${CONFIG_ESPTOOLPY_AFTER}) + set(esptool_args "--esptool-args;before=${CONFIG_ESPTOOLPY_BEFORE};after=${CONFIG_ESPTOOLPY_AFTER}") + set(otatool_args "--partition-table-file;${PARTITION_CSV_PATH};--partition-table-offset;${PARTITION_TABLE_OFFSET}") + idf_component_get_property(esptool_py_dir esptool_py COMPONENT_DIR) add_custom_target(read_otadata DEPENDS "${PARTITION_CSV_PATH}" - COMMAND ${otatool_py} ${esptool_args} - --partition-table-file ${PARTITION_CSV_PATH} - --partition-table-offset ${PARTITION_TABLE_OFFSET} - read_otadata) + COMMAND ${CMAKE_COMMAND} + -D IDF_PATH="${idf_path}" + -D SERIAL_TOOL="${otatool_py}" + -D SERIAL_TOOL_ARGS="${esptool_args};${otatool_args};read_otadata" + -D WORKING_DIRECTORY="${build_dir}" + -P ${esptool_py_dir}/run_serial_tool.cmake + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + USES_TERMINAL + ) add_custom_target(erase_otadata DEPENDS "${PARTITION_CSV_PATH}" - COMMAND ${otatool_py} ${esptool_args} - --partition-table-file ${PARTITION_CSV_PATH} - --partition-table-offset ${PARTITION_TABLE_OFFSET} - erase_otadata) + COMMAND ${CMAKE_COMMAND} + -D IDF_PATH="${idf_path}" + -D SERIAL_TOOL="${otatool_py}" + -D SERIAL_TOOL_ARGS="${esptool_args};${otatool_args};erase_otadata" + -D WORKING_DIRECTORY="${build_dir}" + -P ${esptool_py_dir}/run_serial_tool.cmake + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + USES_TERMINAL + ) idf_component_get_property(main_args esptool_py FLASH_ARGS) idf_component_get_property(sub_args esptool_py FLASH_SUB_ARGS) From 55684fab7245d7a14bc31e7a8bfc85941fd31d90 Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Mon, 12 Oct 2020 22:15:40 +0800 Subject: [PATCH 3/5] esptool_py: use serial script for monitor --- components/esptool_py/project_include.cmake | 6 +++--- components/esptool_py/run_idf_monitor.cmake | 21 --------------------- 2 files changed, 3 insertions(+), 24 deletions(-) delete mode 100644 components/esptool_py/run_idf_monitor.cmake diff --git a/components/esptool_py/project_include.cmake b/components/esptool_py/project_include.cmake index 038a07c937..0297a3a8cd 100644 --- a/components/esptool_py/project_include.cmake +++ b/components/esptool_py/project_include.cmake @@ -143,10 +143,10 @@ add_custom_target(erase_flash add_custom_target(monitor COMMAND ${CMAKE_COMMAND} -D IDF_PATH="${idf_path}" - -D IDF_MONITOR="${idf_path}/tools/idf_monitor.py" - -D ELF_FILE="${elf_dir}/${elf}" + -D SERIAL_TOOL="${idf_path}/tools/idf_monitor.py" + -D SERIAL_TOOL_ARGS="${elf_dir}/${elf}" -D WORKING_DIRECTORY="${build_dir}" - -P run_idf_monitor.cmake + -P run_serial_tool.cmake WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} USES_TERMINAL ) diff --git a/components/esptool_py/run_idf_monitor.cmake b/components/esptool_py/run_idf_monitor.cmake deleted file mode 100644 index 56b58de3da..0000000000 --- a/components/esptool_py/run_idf_monitor.cmake +++ /dev/null @@ -1,21 +0,0 @@ -# A CMake script to run idf_monitor from within ninja or make -# or another cmake-based build runner -# -# (Needed to expand environment variables, for backwards compatibility.) -# -# It is recommended to NOT USE this CMake script if you have the option of -# running idf_monitor.py directly. This script exists only for use inside CMake builds. -# -cmake_minimum_required(VERSION 3.5) - -set(TOOL "idf_monitor.py") - -if(NOT IDF_MONITOR OR NOT ELF_FILE) - message(FATAL_ERROR "IDF_MONITOR and ELF_FILE must " - "be specified on the CMake command line. For direct execution, it is " - "strongly recommended to run ${TOOL} directly.") -endif() - -include("${CMAKE_CURRENT_LIST_DIR}/get_port_args.cmake") -set(CMD "${IDF_MONITOR} ${port_arg} ${baud_arg} ${ELF_FILE}") -include("${CMAKE_CURRENT_LIST_DIR}/run_cmd.cmake") From a9260395751950e4733ed343693fc71b0fc75d86 Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Mon, 12 Oct 2020 22:22:09 +0800 Subject: [PATCH 4/5] esptool_py: simplify serial tool script --- components/app_update/CMakeLists.txt | 3 +- components/esptool_py/get_port_args.cmake | 3 +- components/esptool_py/run_cmd.cmake | 15 ------- components/esptool_py/run_serial_tool.cmake | 50 +++++++++++++++++---- 4 files changed, 45 insertions(+), 26 deletions(-) delete mode 100644 components/esptool_py/run_cmd.cmake diff --git a/components/app_update/CMakeLists.txt b/components/app_update/CMakeLists.txt index a8492e351a..74c0d88fed 100644 --- a/components/app_update/CMakeLists.txt +++ b/components/app_update/CMakeLists.txt @@ -49,7 +49,8 @@ if(NOT BOOTLOADER_BUILD) set(otatool_py ${python} ${COMPONENT_DIR}/otatool.py) set(esptool_args "--esptool-args;before=${CONFIG_ESPTOOLPY_BEFORE};after=${CONFIG_ESPTOOLPY_AFTER}") - set(otatool_args "--partition-table-file;${PARTITION_CSV_PATH};--partition-table-offset;${PARTITION_TABLE_OFFSET}") + set(otatool_args "--partition-table-file;${PARTITION_CSV_PATH}" + "--partition-table-offset;${PARTITION_TABLE_OFFSET}") idf_component_get_property(esptool_py_dir esptool_py COMPONENT_DIR) add_custom_target(read_otadata DEPENDS "${PARTITION_CSV_PATH}" diff --git a/components/esptool_py/get_port_args.cmake b/components/esptool_py/get_port_args.cmake index e70f131046..90f5258611 100644 --- a/components/esptool_py/get_port_args.cmake +++ b/components/esptool_py/get_port_args.cmake @@ -3,7 +3,8 @@ # without needing a CMake re-run) set(ESPPORT $ENV{ESPPORT}) if(NOT ESPPORT) - message("Note: ${SERIAL_TOOL} will search for a serial port. To specify a port, set the ESPPORT environment variable.") + message("Note: ${SERIAL_TOOL} will search for a serial port. " + "To specify a port, set the ESPPORT environment variable.") else() set(port_arg "-p ${ESPPORT}") endif() diff --git a/components/esptool_py/run_cmd.cmake b/components/esptool_py/run_cmd.cmake deleted file mode 100644 index a5620a3420..0000000000 --- a/components/esptool_py/run_cmd.cmake +++ /dev/null @@ -1,15 +0,0 @@ -if(NOT IDF_PATH) - message(FATAL_ERROR "IDF_PATH not set.") -endif() -include("${IDF_PATH}/tools/cmake/utilities.cmake") -spaces2list(CMD) - -execute_process(COMMAND ${CMD} - WORKING_DIRECTORY "${WORKING_DIRECTORY}" - RESULT_VARIABLE result - ) - -if(${result}) - # No way to have CMake silently fail, unfortunately - message(FATAL_ERROR "${SERIAL_TOOL} failed") -endif() diff --git a/components/esptool_py/run_serial_tool.cmake b/components/esptool_py/run_serial_tool.cmake index 04d3f5afdf..ce8bf93d98 100644 --- a/components/esptool_py/run_serial_tool.cmake +++ b/components/esptool_py/run_serial_tool.cmake @@ -1,19 +1,51 @@ -# A CMake script to run esptool commands from within ninja or make -# or another cmake-based build runner -# -# (Needed to expand environment variables, for backwards compatibility.) +# A CMake script to run serial tool commands supporting ESPPORT and +# ESPBAUD environment variables from within ninja or make or another +# cmake-based build runner. # # It is recommended to NOT USE this CMake script if you have the option of -# running esptool.py directly. This script exists only for use inside CMake builds. -# +# running the tool directly. This script exists only for use inside CMake builds. cmake_minimum_required(VERSION 3.5) +if(NOT IDF_PATH) + message(FATAL_ERROR "IDF_PATH not set.") +endif() + if(NOT SERIAL_TOOL OR NOT SERIAL_TOOL_ARGS) message(FATAL_ERROR "SERIAL_TOOL and SERIAL_TOOL_ARGS must " "be specified on the CMake command line. For direct execution, it is " "strongly recommended to run ${SERIAL_TOOL} directly.") endif() -include("${CMAKE_CURRENT_LIST_DIR}/get_port_args.cmake") -set(CMD "${SERIAL_TOOL} ${port_arg} ${baud_arg} ${SERIAL_TOOL_ARGS}") -include("${CMAKE_CURRENT_LIST_DIR}/run_cmd.cmake") +# Main purpose of this script: we can't expand these environment variables in the main IDF CMake build, +# because we want to expand them at flashing time not at CMake runtime (so they can change +# without needing a CMake re-run) +set(ESPPORT $ENV{ESPPORT}) +if(NOT ESPPORT) + message("Note: ${SERIAL_TOOL} will search for a serial port. " + "To specify a port, set the ESPPORT environment variable.") +else() + set(port_arg "-p ${ESPPORT}") +endif() + +set(ESPBAUD $ENV{ESPBAUD}) +if(NOT ESPBAUD) + message("Note: ${SERIAL_TOOL} will attempt to set baud rate automatically. " + "To specify a baud rate, set the ESPBAUD environment variable.") +else() + set(baud_arg "-b ${ESPBAUD}") +endif() + +set(serial_tool_cmd "${SERIAL_TOOL} ${port_arg} ${baud_arg} ${SERIAL_TOOL_ARGS}") + +include("${IDF_PATH}/tools/cmake/utilities.cmake") +spaces2list(serial_tool_cmd) + +execute_process(COMMAND ${serial_tool_cmd} + WORKING_DIRECTORY "${WORKING_DIRECTORY}" + RESULT_VARIABLE result + ) + +if(${result}) + # No way to have CMake silently fail, unfortunately + message(FATAL_ERROR "${SERIAL_TOOL} failed") +endif() From 84d3b0f81d99dc4b7f9466f69b7e67fd7e52493e Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Mon, 14 Dec 2020 17:11:41 +0800 Subject: [PATCH 5/5] otatool: let fail early on exception otatool gives a 'target has no otadata' error instead of the real cause (because of wrong port specified, for example) --- components/app_update/otatool.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/app_update/otatool.py b/components/app_update/otatool.py index 7a087be16d..ca22001015 100755 --- a/components/app_update/otatool.py +++ b/components/app_update/otatool.py @@ -63,8 +63,6 @@ class OtatoolTarget(): self.target.read_partition(OtatoolTarget.OTADATA_PARTITION, temp_file.name) with open(temp_file.name, "rb") as f: self.otadata = f.read() - except Exception: - self.otadata = None finally: os.unlink(temp_file.name)