Skip to content

Commit 5592145

Browse files
fdcavalcantijerpelea
authored andcommitted
tools/espressif: add esptool version check to Espressif build system
1 parent 9d9857a commit 5592145

File tree

6 files changed

+117
-49
lines changed

6 files changed

+117
-49
lines changed

tools/esp32/Config.mk

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ ESPTOOL_FLASH_OPTS := -fs $(FLASH_SIZE) -fm $(FLASH_MODE) -ff $(FLASH_FREQ)
6868

6969
# Configure the variables according to build environment
7070

71+
ESPTOOL_MIN_VERSION := 4.8.0
72+
7173
ifdef ESPTOOL_BINDIR
7274
ifeq ($(CONFIG_ESP32_APP_FORMAT_LEGACY),y)
7375
BL_OFFSET := 0x1000
@@ -169,6 +171,7 @@ endef
169171
# MERGEBIN -- Merge raw binary files into a single file
170172

171173
define MERGEBIN
174+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
172175
$(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
173176
echo "MERGEBIN error: Missing argument for binary files directory."; \
174177
echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -236,13 +239,7 @@ else
236239
ifeq ($(CONFIG_ESP32_APP_FORMAT_LEGACY),y)
237240
define MKIMAGE
238241
$(Q) echo "MKIMAGE: ESP32 binary"
239-
$(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
240-
echo ""; \
241-
echo "esptool.py not found. Please run: \"pip install esptool==4.8.dev4\""; \
242-
echo ""; \
243-
echo "Run make again to create the nuttx.bin image."; \
244-
exit 1; \
245-
fi
242+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
246243
$(Q) if [ -z $(FLASH_SIZE) ]; then \
247244
echo "Missing Flash memory size configuration for the ESP32 chip."; \
248245
exit 1; \
@@ -268,13 +265,7 @@ endef
268265
else
269266
define MKIMAGE
270267
$(Q) echo "MKIMAGE: ESP32 binary"
271-
$(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
272-
echo ""; \
273-
echo "esptool.py not found. Please run: \"pip install esptool==4.8.dev4\""; \
274-
echo ""; \
275-
echo "Run make again to create the nuttx.bin image."; \
276-
exit 1; \
277-
fi
268+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
278269
$(Q) if [ -z $(FLASH_SIZE) ]; then \
279270
echo "Missing Flash memory size configuration."; \
280271
exit 1; \

tools/esp32c3/Config.mk

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ ESPTOOL_FLASH_OPTS := -fs $(FLASH_SIZE) -fm $(FLASH_MODE) -ff $(FLASH_FREQ)
6868

6969
# Configure the variables according to build environment
7070

71+
ESPTOOL_MIN_VERSION := 4.8.0
72+
7173
ifdef ESPTOOL_BINDIR
7274
ifeq ($(CONFIG_ESP32C3_APP_FORMAT_LEGACY),y)
7375
BL_OFFSET := 0x0
@@ -162,6 +164,7 @@ endef
162164
# MERGEBIN -- Merge raw binary files into a single file
163165

164166
define MERGEBIN
167+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
165168
$(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
166169
echo "MERGEBIN error: Missing argument for binary files directory."; \
167170
echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -218,13 +221,7 @@ else
218221
ifeq ($(CONFIG_ESP32C3_APP_FORMAT_LEGACY),y)
219222
define MKIMAGE
220223
$(Q) echo "MKIMAGE: ESP32-C3 binary"
221-
$(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
222-
echo ""; \
223-
echo "esptool.py not found. Please run: \"pip install esptool==4.8.dev4\""; \
224-
echo ""; \
225-
echo "Run make again to create the nuttx.bin image."; \
226-
exit 1; \
227-
fi
224+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
228225
$(Q) if [ -z $(FLASH_SIZE) ]; then \
229226
echo "Missing Flash memory size configuration for the ESP32-C3 chip."; \
230227
exit 1; \

tools/esp32s2/Config.mk

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ ESPTOOL_FLASH_OPTS := -fs $(FLASH_SIZE) -fm $(FLASH_MODE) -ff $(FLASH_FREQ)
6868

6969
# Configure the variables according to build environment
7070

71+
ESPTOOL_MIN_VERSION := 4.8.0
72+
7173
ifdef ESPTOOL_BINDIR
7274
ifeq ($(CONFIG_ESP32S2_APP_FORMAT_MCUBOOT),y)
7375
BL_OFFSET := 0x1000
@@ -153,6 +155,7 @@ endef
153155
# MERGEBIN -- Merge raw binary files into a single file
154156

155157
define MERGEBIN
158+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
156159
$(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
157160
echo "MERGEBIN error: Missing argument for binary files directory."; \
158161
echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -223,13 +226,7 @@ endef
223226
else
224227
define MKIMAGE
225228
$(Q) echo "MKIMAGE: ESP32-S2 binary"
226-
$(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
227-
echo ""; \
228-
echo "esptool.py not found. Please run: \"pip install esptool==4.8.dev4\""; \
229-
echo ""; \
230-
echo "Run make again to create the nuttx.bin image."; \
231-
exit 1; \
232-
fi
229+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
233230
$(Q) if [ -z $(FLASH_SIZE) ]; then \
234231
echo "Missing Flash memory size configuration."; \
235232
exit 1; \

tools/esp32s3/Config.mk

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ ESPTOOL_FLASH_OPTS := -fs $(FLASH_SIZE) -fm $(FLASH_MODE) -ff $(FLASH_FREQ)
6464

6565
# Configure the variables according to build environment
6666

67+
ESPTOOL_MIN_VERSION := 4.8.0
68+
6769
ifdef ESPTOOL_BINDIR
6870
ifeq ($(CONFIG_ESP32S3_APP_FORMAT_LEGACY),y)
6971
BL_OFFSET := 0x0
@@ -118,6 +120,7 @@ endif
118120
# MERGEBIN -- Merge raw binary files into a single file
119121

120122
define MERGEBIN
123+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
121124
$(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
122125
echo "MERGEBIN error: Missing argument for binary files directory."; \
123126
echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -143,13 +146,7 @@ endef
143146
ifeq ($(CONFIG_ESP32S3_APP_FORMAT_LEGACY),y)
144147
define MKIMAGE
145148
$(Q) echo "MKIMAGE: ESP32-S3 binary"
146-
$(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
147-
echo ""; \
148-
echo "esptool.py not found. Please run: \"pip install esptool==4.8.dev4\""; \
149-
echo ""; \
150-
echo "Run make again to create the nuttx.bin image."; \
151-
exit 1; \
152-
fi
149+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
153150
$(Q) if [ -z $(FLASH_SIZE) ]; then \
154151
echo "Missing Flash memory size configuration for the ESP32-S3 chip."; \
155152
exit 1; \
@@ -175,13 +172,7 @@ endef
175172
else
176173
define MKIMAGE
177174
$(Q) echo "MKIMAGE: ESP32-S3 binary"
178-
$(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
179-
echo ""; \
180-
echo "esptool.py not found. Please run: \"pip install esptool==4.8.dev4\""; \
181-
echo ""; \
182-
echo "Run make again to create the nuttx.bin image."; \
183-
exit 1; \
184-
fi
175+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
185176
$(Q) if [ -z $(FLASH_SIZE) ]; then \
186177
echo "Missing Flash memory size configuration."; \
187178
exit 1; \

tools/espressif/Config.mk

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ endif
6868

6969
# Configure the variables according to build environment
7070

71+
ESPTOOL_MIN_VERSION := 4.8.0
72+
7173
ifdef ESPTOOL_BINDIR
7274
ifeq ($(CONFIG_ESPRESSIF_BOOTLOADER_MCUBOOT),y)
7375
BL_OFFSET := 0x0000
@@ -104,6 +106,7 @@ ESPTOOL_BINS += $(FLASH_APP)
104106
# MERGEBIN -- Merge raw binary files into a single file
105107

106108
define MERGEBIN
109+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
107110
$(Q) if [ -z $(ESPTOOL_BINDIR) ]; then \
108111
echo "MERGEBIN error: Missing argument for binary files directory."; \
109112
echo "USAGE: make ESPTOOL_BINDIR=<dir>"; \
@@ -137,13 +140,7 @@ endef
137140
else
138141
define MKIMAGE
139142
$(Q) echo "MKIMAGE: NuttX binary"
140-
$(Q) if ! esptool.py version 1>/dev/null 2>&1; then \
141-
echo ""; \
142-
echo "esptool.py not found. Please run: \"pip install esptool==4.8.dev4\""; \
143-
echo ""; \
144-
echo "Run make again to create the nuttx.bin image."; \
145-
exit 1; \
146-
fi
143+
@python3 tools/espressif/check_esptool.py -v $(ESPTOOL_MIN_VERSION)
147144
$(Q) if [ -z $(FLASH_SIZE) ]; then \
148145
echo "Missing Flash memory size configuration."; \
149146
exit 1; \

tools/espressif/check_esptool.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
############################################################################
2+
# tools/espressif/check_esptool.py
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
import argparse
23+
import re
24+
import sys
25+
26+
# Different package required if Python 3.8+
27+
if sys.version_info.minor < 8:
28+
import pkg_resources
29+
30+
PYTHON_OLDER = True
31+
else:
32+
from importlib.metadata import PackageNotFoundError, version
33+
34+
PYTHON_OLDER = False
35+
36+
37+
parser = argparse.ArgumentParser(
38+
prog="check_esptool",
39+
description="Checks esptool version and returns true or \
40+
false if it matches target version",
41+
)
42+
parser.add_argument("-v", "--version", action="store", required=True)
43+
44+
45+
def parse_version(version_string) -> list:
46+
"""Regex patteren to extract version major and minor."""
47+
pattern = r"(\d+)\.(\d+)"
48+
match = re.search(pattern, version_string)
49+
50+
if match:
51+
major = int(match.group(1))
52+
minor = int(match.group(2))
53+
return [major, minor]
54+
else:
55+
return []
56+
57+
58+
def check_version(min_esptool_version: str) -> int:
59+
"""Attempts to read 'esptool' version using pkg_resources (for
60+
Python <3.8) or importlib. Compare current version with
61+
'min_esptool_version' and returns.
62+
63+
Returns:
64+
True: packages does not exist or outdated
65+
False: package installed and up-to-date
66+
"""
67+
if PYTHON_OLDER:
68+
try:
69+
version_str = pkg_resources.get_distribution("esptool").version
70+
except pkg_resources.DistributionNotFound:
71+
print("esptool.py not found. Please run: 'pip install esptool'")
72+
print("Run make again to create the nuttx.bin image.")
73+
return True
74+
else:
75+
try:
76+
version_str = version("esptool")
77+
except PackageNotFoundError:
78+
print("esptool.py not found. Please run: 'pip install esptool'")
79+
print("Run make again to create the nuttx.bin image.")
80+
return True
81+
82+
esptool_version = parse_version(version_str)
83+
min_esptool_version = parse_version(parser.version)
84+
85+
if esptool_version >= min_esptool_version:
86+
return False
87+
88+
print("Unsupported esptool version:", version_str, "expects >=", parser.version)
89+
print("Upgrade using: 'pip install --upgrade esptool' and run 'make' again")
90+
return True
91+
92+
93+
if __name__ == "__main__":
94+
parser = parser.parse_args()
95+
sys.exit(check_version(parser.version))

0 commit comments

Comments
 (0)