Skip to content

Commit cfaa76b

Browse files
authored
Merge pull request #21625 from crasbe/pr/esptools_export
dist/tools: improve the `esptools`, enhance documentation
2 parents 0005683 + bd0f8b7 commit cfaa76b

File tree

3 files changed

+63
-38
lines changed

3 files changed

+63
-38
lines changed

cpu/esp32/doc.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,13 @@ if you have already used ESP-IDF directly.
574574

575575
Once the ESP32x tools are installed in the directory specified by the
576576
environment variable `$IDF_TOOLS_PATH`, the shell script
577-
`$RIOTBASE/dist/tools/esptools/install.sh` can be sourced to export the
577+
`$RIOTBASE/dist/tools/esptools/export.sh` can be sourced to export the
578578
paths of the installed tools using again the environment variable
579579
`$IDF_TOOLS_PATH`.
580+
By writing `.` followed by a space in front of the script, the script
581+
runs in the same environment as the current shell and sets the local
582+
environment variables, which would otherwise be lost on termination
583+
of the script.
580584

581585
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
582586
$ . dist/tools/esptools/export.sh

dist/tools/esptools/export.sh

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#!/bin/sh
22

3+
# If the script is not sourced, the exported variables are not saved
4+
# in the environment.
5+
if [ "$(basename -- "$0")" = "export.sh" ]; then
6+
echo "Please run the script prefixed with a '.' followed by a space to source it." 1>&2
7+
exit 1
8+
fi
9+
310
ESP32_GCC_RELEASE="esp-14.2.0_20241119"
411
ESP8266_GCC_RELEASE="esp-5.2.0_20191018"
512

@@ -15,6 +22,49 @@ fi
1522

1623
TOOLS_PATH="${IDF_TOOLS_PATH}/tools"
1724

25+
# this function expects the parameters $TOOL, $TOOLS_DIR and $*_VERSION
26+
export_checks()
27+
{
28+
TOOL="$1"
29+
TOOLS_DIR_INT="$2" # internal TOOLS_DIR
30+
TOOLS_VERSION="$3"
31+
32+
# create the wildcard expression from the TOOLS_DIR
33+
TOOLS_DIR_BASE=$(echo "$TOOLS_DIR_INT/bin" | sed "s|/$TOOLS_VERSION/|/[^/]*/|")
34+
TOOLS_DIR_IN_PATH=$(echo "$PATH" | grep "${TOOLS_DIR_INT}")
35+
36+
if [ ! -e "${TOOLS_DIR_INT}" ]; then
37+
echo "${TOOLS_DIR_INT} does not exist - please run"
38+
echo "\${RIOTBASE}/dist/tools/esptools/install.sh $TOOL"
39+
return 1
40+
fi
41+
42+
echo "$PATH" | tr ':' '\n' | while read -r entry; do
43+
if echo "$entry" | grep -q "^${TOOLS_DIR_BASE}$"; then
44+
if [ "$entry" != "${TOOLS_DIR_INT}/bin" ]; then
45+
echo "Warning: PATH contains outdated entry: \"$entry\"." \
46+
"Please check your ~/.bashrc or ~/.profile.">&2
47+
fi
48+
fi
49+
done
50+
unset entry
51+
52+
if [ -e "${TOOLS_DIR_INT}" ] && [ -z "${TOOLS_DIR_IN_PATH}" ]; then
53+
echo "Extending PATH by ${TOOLS_DIR_INT}/bin"
54+
export PATH="${TOOLS_DIR_INT}/bin:${PATH}"
55+
56+
echo "To make this permanent, add this line to your ~/.bashrc or ~/.profile:"
57+
echo PATH="\$PATH:${TOOLS_DIR_INT}/bin"
58+
fi
59+
60+
unset TOOL
61+
unset TOOLS_DIR_INT
62+
unset TOOLS_VERSION
63+
unset TOOLS_DIR_IN_PATH
64+
65+
return 0
66+
}
67+
1868
export_arch()
1969
{
2070
case $1 in
@@ -36,35 +86,18 @@ export_arch()
3686
esac
3787

3888
TOOLS_DIR="${TOOLS_PATH}/${TARGET_ARCH}/${ESP_GCC_RELEASE}/${TARGET_ARCH}"
39-
TOOLS_DIR_IN_PATH=$(echo "$PATH" | grep "${TOOLS_DIR}")
40-
41-
if [ ! -e "${TOOLS_DIR}" ]; then
42-
echo "${TOOLS_DIR} does not exist - please run"
43-
echo "\${RIOTBASE}/dist/tools/esptools/install.sh $1"
44-
return
45-
fi
46-
47-
if [ -e "${TOOLS_DIR}" ] && [ -z "${TOOLS_DIR_IN_PATH}" ]; then
48-
echo "Extending PATH by ${TOOLS_DIR}/bin"
49-
export PATH="${TOOLS_DIR}/bin:${PATH}"
50-
fi
51-
52-
echo "To make this permanent, add this line to your ~/.bashrc or ~/.profile:"
53-
echo PATH="\$PATH:${TOOLS_DIR}/bin"
54-
89+
export_checks "$1" "$TOOLS_DIR" "$ESP_GCC_RELEASE"
5590
unset TOOLS_DIR
5691
}
5792

5893
export_openocd()
5994
{
6095
TOOLS_DIR="${TOOLS_PATH}/openocd-esp32/${ESP32_OPENOCD_VERSION}"
61-
TOOLS_DIR_IN_PATH=$(echo "$PATH" | grep "${TOOLS_DIR}")
6296
OPENOCD_DIR="${TOOLS_DIR}/openocd-esp32"
6397

64-
if [ -e "${OPENOCD_DIR}" ] && [ -z "${TOOLS_DIR_IN_PATH}" ]; then
65-
echo "Extending PATH by ${TOOLS_DIR}/bin"
66-
export PATH="${OPENOCD_DIR}/bin:${PATH}"
67-
export OPENOCD="${OPENOCD_DIR}/bin/openocd -s ${OPENOCD_DIR}/share/openocd/scripts"
98+
export_checks "openocd" "$OPENOCD_DIR" "$ESP32_OPENOCD_VERSION"
99+
if [ $? -eq 0 ]; then
100+
export OPENOCD="${OPENOCD_DIR}/bin/openocd -s ${OPENOCD_DIR}/share/openocd/scripts"
68101
fi
69102

70103
unset TOOLS_DIR
@@ -107,13 +140,7 @@ export_qemu()
107140
fi
108141

109142
TOOLS_DIR="${TOOLS_PATH}/${QEMU_ARCH}/${ESP32_QEMU_VERSION}/qemu"
110-
TOOLS_DIR_IN_PATH=$(echo "$PATH" | grep "${TOOLS_DIR}")
111-
112-
if [ -e "${TOOLS_DIR}" ] && [ -z "${TOOLS_DIR_IN_PATH}" ]; then
113-
echo "Extending PATH by ${TOOLS_DIR}/bin"
114-
export PATH="${TOOLS_DIR}/bin:${PATH}"
115-
fi
116-
143+
export_checks "qemu $1" "$TOOLS_DIR" "$ESP32_QEMU_VERSION"
117144
unset TOOLS_DIR
118145
}
119146

@@ -132,13 +159,7 @@ export_gdb()
132159
esac
133160

134161
TOOLS_DIR="${TOOLS_PATH}/${GDB_ARCH}/${GDB_VERSION}/${GDB_ARCH}"
135-
TOOLS_DIR_IN_PATH=$(echo "$PATH" | grep "${TOOLS_DIR}")
136-
137-
if [ -e "${TOOLS_DIR}" ] && [ -z "${TOOLS_DIR_IN_PATH}" ]; then
138-
echo "Extending PATH by ${TOOLS_DIR}/bin"
139-
export PATH="${TOOLS_DIR}/bin:${PATH}"
140-
fi
141-
162+
export_checks "gdb $1" "$TOOLS_DIR" "$GDB_VERSION"
142163
unset TOOLS_DIR
143164
}
144165

@@ -170,7 +191,7 @@ elif [ "$1" = "gdb" ]; then
170191
elif [ "$1" = "openocd" ]; then
171192
export_openocd
172193
elif [ "$1" = "qemu" ]; then
173-
export_qemu $2
194+
export_qemu "$2"
174195
else
175196
export_arch "$1"
176197
fi

dist/tools/esptools/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,4 @@ else
264264
fi
265265

266266
echo "Use following command to extend the PATH variable:"
267-
echo ". $(dirname "$0")/export.sh $1"
267+
echo ". $(dirname "$0")/export.sh $1 $2"

0 commit comments

Comments
 (0)