Skip to content

Commit 8889a1b

Browse files
committed
fix(stm32cubeprog): harden getopt support on MacOS
- brew prefix usage - MacPorts support Fixes #115 Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent fe1ffaa commit 8889a1b

1 file changed

Lines changed: 68 additions & 15 deletions

File tree

stm32CubeProg.sh

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -o nounset # Treat unset variables as an error
33
# set -o xtrace # Print command traces before executing command.
44

55
UNAME_OS="$(uname -s)"
6-
GNU_GETOPT=
6+
GNU_GETOPT="n" # "y" if GNU getopt is available
77
STM32CP_CLI=
88
INTERFACE=
99
PORT=
@@ -69,7 +69,68 @@ aborting() {
6969
exit 1
7070
}
7171

72-
# Check STM32CubeProgrammer cli availability and getopt version
72+
check_getopt() {
73+
case "${UNAME_OS}" in
74+
Linux* | Windows*)
75+
GNU_GETOPT="y"
76+
;;
77+
Darwin*)
78+
# Check if getopt is in the path and if it is gnu or BSD version in the path
79+
if command -v getopt >/dev/null 2>&1; then
80+
getopt --test >/dev/null 2>&1
81+
if [ $? -eq 4 ]; then
82+
GNU_GETOPT="y"
83+
fi
84+
fi
85+
# If getopt is not gnu version, check if it is installed via brew
86+
if [ "${GNU_GETOPT}" != "y" ]; then
87+
if command -v brew >/dev/null 2>&1; then
88+
BREW_PREFIX=$(brew --prefix)
89+
if command -v "${BREW_PREFIX}/bin/getopt" >/dev/null 2>&1; then
90+
"${BREW_PREFIX}"/bin/getopt --test >/dev/null 2>&1
91+
if [ $? -eq 4 ]; then
92+
export PATH="${BREW_PREFIX}/bin:$PATH"
93+
GNU_GETOPT="y"
94+
fi
95+
elif command -v "${BREW_PREFIX}/opt/gnu-getopt/bin/getopt" >/dev/null 2>&1; then
96+
export PATH="${BREW_PREFIX}/opt/gnu-getopt/bin":"$PATH"
97+
GNU_GETOPT="y"
98+
fi
99+
fi
100+
# Check for MacPorts getopt
101+
if [ "${GNU_GETOPT}" != "y" ]; then
102+
if command -v /opt/local/bin/getopt >/dev/null 2>&1; then
103+
/opt/local/bin/getopt --test >/dev/null 2>&1
104+
if [ $? -eq 4 ]; then
105+
export PATH="/opt/local/bin:$PATH"
106+
GNU_GETOPT="y"
107+
fi
108+
fi
109+
fi
110+
# Fallback to previous check
111+
if [ "${GNU_GETOPT}" != "y" ]; then
112+
if ! command -v /usr/local/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
113+
if command -v /opt/homebrew/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
114+
export PATH="/opt/homebrew/opt/gnu-getopt/bin":"$PATH"
115+
GNU_GETOPT="y"
116+
fi
117+
else
118+
export PATH="/usr/local/opt/gnu-getopt/bin":"$PATH"
119+
GNU_GETOPT="y"
120+
fi
121+
fi
122+
fi
123+
;;
124+
*)
125+
echo "Unknown host OS: ${UNAME_OS}." >&2
126+
exit 1
127+
;;
128+
esac
129+
}
130+
131+
check_getopt
132+
133+
# Check STM32CubeProgrammer cli availability
73134
case "${UNAME_OS}" in
74135
Linux*)
75136
STM32CP_CLI=STM32_Programmer.sh
@@ -91,16 +152,6 @@ case "${UNAME_OS}" in
91152
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
92153
aborting
93154
fi
94-
if ! command -v /usr/local/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
95-
if ! command -v /opt/homebrew/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
96-
echo "Warning: long options not supported due to getopt from FreeBSD usage."
97-
GNU_GETOPT=n
98-
else
99-
export PATH="/opt/homebrew/opt/gnu-getopt/bin":"$PATH"
100-
fi
101-
else
102-
export PATH="/usr/local/opt/gnu-getopt/bin":"$PATH"
103-
fi
104155
;;
105156
Windows*)
106157
STM32CP_CLI=STM32_Programmer_CLI.exe
@@ -126,13 +177,15 @@ esac
126177

127178
# parse command line arguments
128179
# options may be followed by one colon to indicate they have a required arg
129-
if [ -n "${GNU_GETOPT}" ]; then
130-
if ! options=$(getopt hi:b:a:es:o:f:m:n:c:d:p:r:v: "$@"); then
180+
if [ "${GNU_GETOPT}" = "y" ]; then
181+
if ! options=$(getopt -a -o hi:b:a:es:o:f:m:n:c:d:p:r:v: --long help,interface:,bin:,address:,erase,start:,offset:,freq:,mode:,snum:,com:,dtr:,parity:,rts:,pid:,vid: -- "$@"); then
131182
echo "Terminating..." >&2
132183
exit 1
133184
fi
134185
else
135-
if ! options=$(getopt -a -o hi:b:a:es:o:f:m:n:c:d:p:r:v: --long help,interface:,bin:,address:,erase,start:,offset:,freq:,mode:,snum:,com:,dtr:,parity:,rts:,pid:,vid: -- "$@"); then
186+
echo "Warning: long options not supported due to getopt from FreeBSD usage."
187+
echo " Space in arguments is not supported."
188+
if ! options=$(getopt hi:b:a:es:o:f:m:n:c:d:p:r:v: "$@"); then
136189
echo "Terminating..." >&2
137190
exit 1
138191
fi

0 commit comments

Comments
 (0)