Skip to content

Commit 1c5f1bd

Browse files
committed
fix(stm32CubeProg): use BSD getopt for MacOS
By default, MacOS uses getopt from FreeBSD and not the GNU-based one. Then "-o" and long options are not supported except if gnu-getopt is installed. Fixes #99 Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 354699b commit 1c5f1bd

File tree

1 file changed

+67
-48
lines changed

1 file changed

+67
-48
lines changed

stm32CubeProg.sh

+67-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,69 @@ 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+
echo "Warning: long options not supported due to getopt from FreeBSD usage."
82+
GNU_GETOPT=n
83+
else
84+
export PATH="/usr/local/opt/gnu-getopt/bin/":"$PATH"
85+
fi
86+
;;
87+
Windows*)
88+
STM32CP_CLI=STM32_Programmer_CLI.exe
89+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
90+
if [ -n "${PROGRAMFILES+x}" ]; then
91+
STM32CP86=${PROGRAMFILES}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
92+
export PATH="${STM32CP86}":"$PATH"
93+
fi
94+
if [ -n "${PROGRAMW6432+x}" ]; then
95+
STM32CP=${PROGRAMW6432}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
96+
export PATH="${STM32CP}":"$PATH"
97+
fi
98+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
99+
aborting
100+
fi
101+
fi
102+
;;
103+
*)
104+
echo "Unknown host OS: ${UNAME_OS}." >&2
105+
exit 1
106+
;;
107+
esac
108+
56109
# parse command line arguments
57110
# 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
111+
if [ -n "${GNU_GETOPT}" ]; then
112+
if ! options=$(getopt hi:ef:o:c:r:d:v:p: "$@"); then
113+
echo "Terminating..." >&2
114+
exit 1
115+
fi
116+
else
117+
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
118+
echo "Terminating..." >&2
119+
exit 1
120+
fi
61121
fi
62122

63123
eval set -- "$options"
@@ -109,53 +169,12 @@ while true; do
109169
shift
110170
break
111171
;;
172+
*)
173+
echo "Unknown option $1"
174+
usage 1
175+
;;
112176
esac
113177
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
159178

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

0 commit comments

Comments
 (0)