Skip to content

Commit adcb573

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 adcb573

1 file changed

Lines changed: 40 additions & 11 deletions

File tree

‎stm32CubeProg.sh‎

Lines changed: 40 additions & 11 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=y
77
STM32CP_CLI=
88
INTERFACE=
99
PORT=
@@ -91,15 +91,42 @@ case "${UNAME_OS}" in
9191
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
9292
aborting
9393
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."
94+
# Manage getopt
95+
# Check if getopt is in the path and if it is gnu or BSD version in the path
96+
if command -v getopt >/dev/null 2>&1; then
97+
if getopt --test 2>/dev/null; then
9798
GNU_GETOPT=n
98-
else
99-
export PATH="/opt/homebrew/opt/gnu-getopt/bin":"$PATH"
10099
fi
101-
else
102-
export PATH="/usr/local/opt/gnu-getopt/bin":"$PATH"
100+
fi
101+
# If getopt is not gnu version, check if it is installed via brew or macports and add it to the path
102+
if [ "${GNU_GETOPT}" != "y" ]; then
103+
if command -v brew >/dev/null 2>&1; then
104+
BREW_PREFIX=$(brew --prefix)
105+
# Only check in bin as brew symlinks its files into the prefix,
106+
if command -v "${BREW_PREFIX}/getopt" >/dev/null 2>&1; then
107+
export PATH="${BREW_PREFIX}/bin":"$PATH"
108+
GNU_GETOPT=y
109+
fi
110+
fi
111+
# Check if GNU_GETOPT is set then check for MacPorts getopt
112+
if [ "${GNU_GETOPT}" != "y" ]; then
113+
if command -v /opt/local/bin/getopt >/dev/null 2>&1; then
114+
export PATH="/opt/local/bin:$PATH"
115+
GNU_GETOPT=y
116+
fi
117+
fi
118+
# Fallback to previous check
119+
if [ "${GNU_GETOPT}" != "y" ]; then
120+
if ! command -v /usr/local/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
121+
if command -v /opt/homebrew/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
122+
export PATH="/opt/homebrew/opt/gnu-getopt/bin":"$PATH"
123+
GNU_GETOPT=y
124+
fi
125+
else
126+
export PATH="/usr/local/opt/gnu-getopt/bin":"$PATH"
127+
GNU_GETOPT=y
128+
fi
129+
fi
103130
fi
104131
;;
105132
Windows*)
@@ -126,13 +153,15 @@ esac
126153

127154
# parse command line arguments
128155
# 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
156+
if [ "${GNU_GETOPT}" = "y" ]; then
157+
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
131158
echo "Terminating..." >&2
132159
exit 1
133160
fi
134161
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
162+
echo "Warning: long options not supported due to getopt from FreeBSD usage."
163+
echo " Space in arguments is not supported."
164+
if ! options=$(getopt hi:b:a:es:o:f:m:n:c:d:p:r:v: "$@"); then
136165
echo "Terminating..." >&2
137166
exit 1
138167
fi

0 commit comments

Comments
 (0)