This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch releases/12.7
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/releases/12.7 by this push:
     new 71d57aac25 tools/espressif: add esptool version check to Espressif 
build system
71d57aac25 is described below

commit 71d57aac25b64b2a771b897ac2fde81487f64e58
Author: Filipe Cavalcanti <filipe.cavalca...@espressif.com>
AuthorDate: Mon Oct 14 16:44:45 2024 +0200

    tools/espressif: add esptool version check to Espressif build system
---
 tools/esp32/Config.mk            | 19 +++-----
 tools/esp32c3/Config.mk          | 11 ++---
 tools/esp32s2/Config.mk          | 11 ++---
 tools/esp32s3/Config.mk          | 19 +++-----
 tools/espressif/Config.mk        | 11 ++---
 tools/espressif/check_esptool.py | 95 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 117 insertions(+), 49 deletions(-)

diff --git a/tools/esp32/Config.mk b/tools/esp32/Config.mk
index 02ca4dd666..7f155682a1 100644
--- a/tools/esp32/Config.mk
+++ b/tools/esp32/Config.mk
@@ -68,6 +68,8 @@ ESPTOOL_FLASH_OPTS := -fs $(FLASH_SIZE) -fm $(FLASH_MODE) -ff 
$(FLASH_FREQ)
 
 # Configure the variables according to build environment
 
+ESPTOOL_MIN_VERSION := 4.8.0
+
 ifdef ESPTOOL_BINDIR
        ifeq ($(CONFIG_ESP32_APP_FORMAT_LEGACY),y)
                BL_OFFSET       := 0x1000
@@ -169,6 +171,7 @@ endef
 # MERGEBIN -- Merge raw binary files into a single file
 
 define MERGEBIN
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
                echo "MERGEBIN error: Missing argument for binary files 
directory."; \
                echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -236,13 +239,7 @@ else
 ifeq ($(CONFIG_ESP32_APP_FORMAT_LEGACY),y)
 define MKIMAGE
        $(Q) echo "MKIMAGE: ESP32 binary"
-       $(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
-               echo ""; \
-               echo "esptool.py not found.  Please run: \"pip install 
esptool==4.8.dev4\""; \
-               echo ""; \
-               echo "Run make again to create the nuttx.bin image."; \
-               exit 1; \
-       fi
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(FLASH_SIZE) ]; then \
                echo "Missing Flash memory size configuration for the ESP32 
chip."; \
                exit 1; \
@@ -268,13 +265,7 @@ endef
 else
 define MKIMAGE
        $(Q) echo "MKIMAGE: ESP32 binary"
-       $(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
-               echo ""; \
-               echo "esptool.py not found.  Please run: \"pip install 
esptool==4.8.dev4\""; \
-               echo ""; \
-               echo "Run make again to create the nuttx.bin image."; \
-               exit 1; \
-       fi
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(FLASH_SIZE) ]; then \
                echo "Missing Flash memory size configuration."; \
                exit 1; \
diff --git a/tools/esp32c3/Config.mk b/tools/esp32c3/Config.mk
index 08b8c78866..4d229b748b 100644
--- a/tools/esp32c3/Config.mk
+++ b/tools/esp32c3/Config.mk
@@ -68,6 +68,8 @@ ESPTOOL_FLASH_OPTS := -fs $(FLASH_SIZE) -fm $(FLASH_MODE) -ff 
$(FLASH_FREQ)
 
 # Configure the variables according to build environment
 
+ESPTOOL_MIN_VERSION := 4.8.0
+
 ifdef ESPTOOL_BINDIR
        ifeq ($(CONFIG_ESP32C3_APP_FORMAT_LEGACY),y)
                BL_OFFSET       := 0x0
@@ -162,6 +164,7 @@ endef
 # MERGEBIN -- Merge raw binary files into a single file
 
 define MERGEBIN
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
                echo "MERGEBIN error: Missing argument for binary files 
directory."; \
                echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -218,13 +221,7 @@ else
 ifeq ($(CONFIG_ESP32C3_APP_FORMAT_LEGACY),y)
 define MKIMAGE
        $(Q) echo "MKIMAGE: ESP32-C3 binary"
-       $(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
-               echo ""; \
-               echo "esptool.py not found.  Please run: \"pip install 
esptool==4.8.dev4\""; \
-               echo ""; \
-               echo "Run make again to create the nuttx.bin image."; \
-               exit 1; \
-       fi
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(FLASH_SIZE) ]; then \
                echo "Missing Flash memory size configuration for the ESP32-C3 
chip."; \
                exit 1; \
diff --git a/tools/esp32s2/Config.mk b/tools/esp32s2/Config.mk
index 3c2468c0e9..78b4cefe44 100644
--- a/tools/esp32s2/Config.mk
+++ b/tools/esp32s2/Config.mk
@@ -68,6 +68,8 @@ ESPTOOL_FLASH_OPTS := -fs $(FLASH_SIZE) -fm $(FLASH_MODE) -ff 
$(FLASH_FREQ)
 
 # Configure the variables according to build environment
 
+ESPTOOL_MIN_VERSION := 4.8.0
+
 ifdef ESPTOOL_BINDIR
        ifeq ($(CONFIG_ESP32S2_APP_FORMAT_MCUBOOT),y)
                BL_OFFSET        := 0x1000
@@ -153,6 +155,7 @@ endef
 # MERGEBIN -- Merge raw binary files into a single file
 
 define MERGEBIN
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
                echo "MERGEBIN error: Missing argument for binary files 
directory."; \
                echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -223,13 +226,7 @@ endef
 else
 define MKIMAGE
        $(Q) echo "MKIMAGE: ESP32-S2 binary"
-       $(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
-               echo ""; \
-               echo "esptool.py not found.  Please run: \"pip install 
esptool==4.8.dev4\""; \
-               echo ""; \
-               echo "Run make again to create the nuttx.bin image."; \
-               exit 1; \
-       fi
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(FLASH_SIZE) ]; then \
                echo "Missing Flash memory size configuration."; \
                exit 1; \
diff --git a/tools/esp32s3/Config.mk b/tools/esp32s3/Config.mk
index 3ca37ea260..d295c64345 100644
--- a/tools/esp32s3/Config.mk
+++ b/tools/esp32s3/Config.mk
@@ -64,6 +64,8 @@ ESPTOOL_FLASH_OPTS := -fs $(FLASH_SIZE) -fm $(FLASH_MODE) -ff 
$(FLASH_FREQ)
 
 # Configure the variables according to build environment
 
+ESPTOOL_MIN_VERSION := 4.8.0
+
 ifdef ESPTOOL_BINDIR
        ifeq ($(CONFIG_ESP32S3_APP_FORMAT_LEGACY),y)
                BL_OFFSET       := 0x0
@@ -118,6 +120,7 @@ endif
 # MERGEBIN -- Merge raw binary files into a single file
 
 define MERGEBIN
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
                echo "MERGEBIN error: Missing argument for binary files 
directory."; \
                echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -137,13 +140,7 @@ endef
 ifeq ($(CONFIG_ESP32S3_APP_FORMAT_LEGACY),y)
 define MKIMAGE
        $(Q) echo "MKIMAGE: ESP32-S3 binary"
-       $(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
-               echo ""; \
-               echo "esptool.py not found.  Please run: \"pip install 
esptool==4.8.dev4\""; \
-               echo ""; \
-               echo "Run make again to create the nuttx.bin image."; \
-               exit 1; \
-       fi
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(FLASH_SIZE) ]; then \
                echo "Missing Flash memory size configuration for the ESP32-S3 
chip."; \
                exit 1; \
@@ -169,13 +166,7 @@ endef
 else
 define MKIMAGE
        $(Q) echo "MKIMAGE: ESP32-S3 binary"
-       $(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
-               echo ""; \
-               echo "esptool.py not found.  Please run: \"pip install 
esptool==4.8.dev4\""; \
-               echo ""; \
-               echo "Run make again to create the nuttx.bin image."; \
-               exit 1; \
-       fi
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(FLASH_SIZE) ]; then \
                echo "Missing Flash memory size configuration."; \
                exit 1; \
diff --git a/tools/espressif/Config.mk b/tools/espressif/Config.mk
index f6afb0995c..4e76bd1d0c 100644
--- a/tools/espressif/Config.mk
+++ b/tools/espressif/Config.mk
@@ -68,6 +68,8 @@ endif
 
 # Configure the variables according to build environment
 
+ESPTOOL_MIN_VERSION := 4.8.0
+
 ifdef ESPTOOL_BINDIR
        ifeq ($(CONFIG_ESPRESSIF_BOOTLOADER_MCUBOOT),y)
                BL_OFFSET       := 0x0000
@@ -104,6 +106,7 @@ ESPTOOL_BINS += $(FLASH_APP)
 # MERGEBIN -- Merge raw binary files into a single file
 
 define MERGEBIN
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
                echo "MERGEBIN error: Missing argument for binary files 
directory."; \
                echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -137,13 +140,7 @@ endef
 else
 define MKIMAGE
        $(Q) echo "MKIMAGE: NuttX binary"
-       $(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
-               echo ""; \
-               echo "esptool.py not found.  Please run: \"pip install 
esptool==4.8.dev4\""; \
-               echo ""; \
-               echo "Run make again to create the nuttx.bin image."; \
-               exit 1; \
-       fi
+       @python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
        $(Q) if [ -z $(FLASH_SIZE) ]; then \
                echo "Missing Flash memory size configuration."; \
                exit 1; \
diff --git a/tools/espressif/check_esptool.py b/tools/espressif/check_esptool.py
new file mode 100644
index 0000000000..439dfd8654
--- /dev/null
+++ b/tools/espressif/check_esptool.py
@@ -0,0 +1,95 @@
+############################################################################
+# tools/espressif/check_esptool.py
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+import argparse
+import re
+import sys
+
+# Different package required if Python 3.8+
+if sys.version_info.minor < 8:
+    import pkg_resources
+
+    PYTHON_OLDER = True
+else:
+    from importlib.metadata import PackageNotFoundError, version
+
+    PYTHON_OLDER = False
+
+
+parser = argparse.ArgumentParser(
+    prog="check_esptool",
+    description="Checks esptool version and returns true or \
+    false if it matches target version",
+)
+parser.add_argument("-v", "--version", action="store", required=True)
+
+
+def parse_version(version_string) -> list:
+    """Regex patteren to extract version major and minor."""
+    pattern = r"(\d+)\.(\d+)"
+    match = re.search(pattern, version_string)
+
+    if match:
+        major = int(match.group(1))
+        minor = int(match.group(2))
+        return [major, minor]
+    else:
+        return []
+
+
+def check_version(min_esptool_version: str) -> int:
+    """Attempts to read 'esptool' version using pkg_resources (for
+    Python <3.8) or importlib. Compare current version with
+    'min_esptool_version' and returns.
+
+    Returns:
+        True: packages does not exist or outdated
+        False: package installed and up-to-date
+    """
+    if PYTHON_OLDER:
+        try:
+            version_str = pkg_resources.get_distribution("esptool").version
+        except pkg_resources.DistributionNotFound:
+            print("esptool.py not found. Please run: 'pip install esptool'")
+            print("Run make again to create the nuttx.bin image.")
+            return True
+    else:
+        try:
+            version_str = version("esptool")
+        except PackageNotFoundError:
+            print("esptool.py not found. Please run: 'pip install esptool'")
+            print("Run make again to create the nuttx.bin image.")
+            return True
+
+    esptool_version = parse_version(version_str)
+    min_esptool_version = parse_version(parser.version)
+
+    if esptool_version >= min_esptool_version:
+        return False
+
+    print("Unsupported esptool version:", version_str, "expects >=", 
parser.version)
+    print("Upgrade using: 'pip install --upgrade esptool' and run 'make' 
again")
+    return True
+
+
+if __name__ == "__main__":
+    parser = parser.parse_args()
+    sys.exit(check_version(parser.version))

Reply via email to