Skip to content

Commit 17a14b7

Browse files
authoredJul 17, 2024
Merge pull request #100 from fpistm/getopt
fix(stm32CubeProg): fallback to BSD getopt on mac
2 parents 354699b + 8de264f commit 17a14b7

File tree

1 file changed

+71
-48
lines changed

1 file changed

+71
-48
lines changed
 

‎stm32CubeProg.sh

+71-48
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
set -o nounset # Treat unset variables as an error
33
# set -o xtrace # Print command traces before executing command.
44

5+
UNAME_OS="$(uname -s)"
6+
GNU_GETOPT=
57
STM32CP_CLI=
68
INTERFACE=
79
PORT=
@@ -53,11 +55,73 @@ aborting() {
5355
exit 1
5456
}
5557

58+
# Check STM32CubeProgrammer cli availability and getopt version
59+
case "${UNAME_OS}" in
60+
Linux*)
61+
STM32CP_CLI=STM32_Programmer.sh
62+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
63+
export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":"$PATH"
64+
fi
65+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
66+
export PATH="/opt/stm32cubeprog/bin":"$PATH"
67+
fi
68+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
69+
aborting
70+
fi
71+
;;
72+
Darwin*)
73+
STM32CP_CLI=STM32_Programmer_CLI
74+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
75+
export PATH="/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin":"$PATH"
76+
fi
77+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
78+
aborting
79+
fi
80+
if ! command -v /usr/local/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
81+
if ! command -v /opt/homebrew/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
82+
echo "Warning: long options not supported due to getopt from FreeBSD usage."
83+
GNU_GETOPT=n
84+
else
85+
export PATH="/opt/homebrew/opt/gnu-getopt/bin":"$PATH"
86+
fi
87+
else
88+
export PATH="/usr/local/opt/gnu-getopt/bin":"$PATH"
89+
fi
90+
;;
91+
Windows*)
92+
STM32CP_CLI=STM32_Programmer_CLI.exe
93+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
94+
if [ -n "${PROGRAMFILES+x}" ]; then
95+
STM32CP86=${PROGRAMFILES}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
96+
export PATH="${STM32CP86}":"$PATH"
97+
fi
98+
if [ -n "${PROGRAMW6432+x}" ]; then
99+
STM32CP=${PROGRAMW6432}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
100+
export PATH="${STM32CP}":"$PATH"
101+
fi
102+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
103+
aborting
104+
fi
105+
fi
106+
;;
107+
*)
108+
echo "Unknown host OS: ${UNAME_OS}." >&2
109+
exit 1
110+
;;
111+
esac
112+
56113
# parse command line arguments
57114
# options may be followed by one colon to indicate they have a required arg
58-
if ! options=$(getopt -a -o hi:ef:o:c:r:d:v:p: --long help,interface:,erase,file:,offset:,com:,rts:,dtr:,vid:,pid: -- "$@"); then
59-
echo "Terminating..." >&2
60-
exit 1
115+
if [ -n "${GNU_GETOPT}" ]; then
116+
if ! options=$(getopt hi:ef:o:c:r:d:v:p: "$@"); then
117+
echo "Terminating..." >&2
118+
exit 1
119+
fi
120+
else
121+
if ! options=$(getopt -a -o hi:ef:o:c:r:d:v:p: --long help,interface:,erase,file:,offset:,com:,rts:,dtr:,vid:,pid: -- "$@"); then
122+
echo "Terminating..." >&2
123+
exit 1
124+
fi
61125
fi
62126

63127
eval set -- "$options"
@@ -109,53 +173,12 @@ while true; do
109173
shift
110174
break
111175
;;
176+
*)
177+
echo "Unknown option $1"
178+
usage 1
179+
;;
112180
esac
113181
done
114-
# Check STM32CubeProgrammer cli availability, fallback to dfu-util if protocol dfu
115-
UNAME_OS="$(uname -s)"
116-
case "${UNAME_OS}" in
117-
Linux*)
118-
STM32CP_CLI=STM32_Programmer.sh
119-
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
120-
export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":"$PATH"
121-
fi
122-
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
123-
export PATH="/opt/stm32cubeprog/bin":"$PATH"
124-
fi
125-
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
126-
aborting
127-
fi
128-
;;
129-
Darwin*)
130-
STM32CP_CLI=STM32_Programmer_CLI
131-
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
132-
export PATH="/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin":"$PATH"
133-
fi
134-
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
135-
aborting
136-
fi
137-
;;
138-
Windows*)
139-
STM32CP_CLI=STM32_Programmer_CLI.exe
140-
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
141-
if [ -n "${PROGRAMFILES+x}" ]; then
142-
STM32CP86=${PROGRAMFILES}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
143-
export PATH="${STM32CP86}":"$PATH"
144-
fi
145-
if [ -n "${PROGRAMW6432+x}" ]; then
146-
STM32CP=${PROGRAMW6432}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
147-
export PATH="${STM32CP}":"$PATH"
148-
fi
149-
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
150-
aborting
151-
fi
152-
fi
153-
;;
154-
*)
155-
echo "Unknown host OS: ${UNAME_OS}." >&2
156-
exit 1
157-
;;
158-
esac
159182

160183
# Check mandatory options
161184
if [ -z "${INTERFACE}" ]; then

0 commit comments

Comments
 (0)
Failed to load comments.