Skip to content

Commit 1cebe3f

Browse files
committed
Add curl fallback to posix script + improve Windows error handling for dl
Signed-off-by: paulober <[email protected]>
1 parent 303c904 commit 1cebe3f

File tree

2 files changed

+94
-9
lines changed

2 files changed

+94
-9
lines changed

scripts/template_setup_posix

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ check_command()
4848
fi
4949
}
5050

51+
# Silently check if command is available (for fallback logic)
52+
check_command_silent()
53+
{
54+
which $1 &> /dev/null
55+
return $?
56+
}
57+
5158
# Check if the current installation is a full SDK with all GNU toolchains
5259
check_full_gnu_sdk()
5360
{
@@ -60,6 +67,31 @@ check_full_gnu_sdk()
6067
return 0
6168
}
6269

70+
# Download helper function
71+
# Usage: download URL OUTFILE
72+
download()
73+
{
74+
local url="$1"
75+
local outfile="$2"
76+
77+
if [ "${dl_tool}" = "wget" ]; then
78+
# Emulate quiet+progress+timestamping
79+
wget -q --show-progress -N -O "${outfile}" "${url}"
80+
return $?
81+
else
82+
# curl: -f fail on HTTP errors; -L follow redirects
83+
# --remote-time preserves Last-Modified on the file
84+
# -z OUTFILE does conditional GET (only download if newer)
85+
# --progress-bar shows progress
86+
if [ -f "${outfile}" ]; then
87+
curl -fL --retry 5 --retry-delay 2 --progress-bar --remote-time -z "${outfile}" -o "${outfile}" "${url}"
88+
else
89+
curl -fL --retry 5 --retry-delay 2 --progress-bar --remote-time -o "${outfile}" "${url}"
90+
fi
91+
return $?
92+
fi
93+
}
94+
6395
# Display script usage
6496
usage()
6597
{
@@ -70,6 +102,7 @@ usage()
70102
echo " -l Install LLVM toolchain"
71103
echo " -h Install host tools"
72104
echo " -c Register Zephyr SDK CMake package"
105+
echo " -dl <curl|wget> Force downloader (default: wget, fallback to curl)"
73106
echo
74107
echo "Supported GNU Toolchains:"
75108
echo
@@ -159,6 +192,17 @@ else
159192
-c)
160193
do_cmake_pkg="y"
161194
;;
195+
-dl)
196+
shift
197+
if [[ "$1" = "curl" ]]; then
198+
dl_force="curl"
199+
elif [[ "$1" = "wget" ]]; then
200+
dl_force="wget"
201+
else
202+
echo "ERROR: -dl expects <curl|wget>"
203+
exit 3
204+
fi
205+
;;
162206
'-?')
163207
usage
164208
exit 0
@@ -207,7 +251,43 @@ echo
207251

208252
# Check dependencies
209253
check_command cmake 90
210-
check_command wget 91
254+
255+
# Choose downloader (default: wget; fallback to curl). Allow -dl override.
256+
dl_tool=""
257+
258+
if [ "${dl_force}" = "curl" ]; then
259+
check_command_silent curl
260+
if [ $? -eq 0 ]; then
261+
dl_tool="curl"
262+
else
263+
echo "ERROR: -dl curl requested but 'curl' not found in PATH."
264+
exit 91
265+
fi
266+
elif [ "${dl_force}" = "wget" ]; then
267+
check_command_silent wget
268+
if [ $? -eq 0 ]; then
269+
dl_tool="wget"
270+
else
271+
echo "ERROR: -dl wget requested but 'wget' not found in PATH."
272+
exit 91
273+
fi
274+
else
275+
# Default behavior: prefer wget, else curl
276+
check_command_silent wget
277+
if [ $? -eq 0 ]; then
278+
dl_tool="wget"
279+
else
280+
check_command_silent curl
281+
if [ $? -eq 0 ]; then
282+
dl_tool="curl"
283+
fi
284+
fi
285+
fi
286+
287+
if [ -z "${dl_tool}" ]; then
288+
echo "Zephyr SDK setup requires either 'wget' or 'curl' in PATH."
289+
exit 91
290+
fi
211291

212292
# Ask for user inputs if no argument is specified
213293
if [ "${interactive}" = "y" ]; then
@@ -232,7 +312,7 @@ if [ "${do_gnu_toolchain}" = "y" ]; then
232312
echo "Installing '${toolchain}' GNU toolchain ..."
233313

234314
# Download toolchain archive
235-
wget -q --show-progress -N -O "${toolchain_filename}" "${toolchain_uri}"
315+
download "${toolchain_uri}" "${toolchain_filename}"
236316
if [ $? != 0 ]; then
237317
rm -f "${toolchain_filename}"
238318
echo "ERROR: GNU toolchain download failed"
@@ -259,7 +339,7 @@ if [ "${do_llvm_toolchain}" = "y" ] && [ ! -d "llvm" ]; then
259339
toolchain_uri="${dl_rel_base}/${toolchain_filename}"
260340

261341
# Download toolchain archive
262-
wget -q --show-progress -N -O "${toolchain_filename}" "${toolchain_uri}"
342+
download "${toolchain_uri}" "${toolchain_filename}"
263343
if [ $? != 0 ]; then
264344
rm -f "${toolchain_filename}"
265345
echo "ERROR: LLVM toolchain download failed"

scripts/template_setup_win

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ if [%ERRORLEVEL%] neq [0] (
2121
)
2222
exit /b 0
2323

24+
REM # Silently check if command is available (for fallback logic)
25+
:check_command_silent
26+
where %1 >nul 2>&1
27+
exit /b %ERRORLEVEL%
28+
2429
:entry
2530

2631
REM # Initialise GNU toolchain list
@@ -184,7 +189,7 @@ set DL_TOOL=
184189

185190
if /i [%DL_FORCE%] equ [curl] (
186191
call :check_command curl 91
187-
if [%ERRORLEVEL%] equ [0] (
192+
if [!ERRORLEVEL!] equ [0] (
188193
set DL_TOOL=curl
189194
) else (
190195
echo ERROR: /dl curl requested but 'curl' not found in PATH.
@@ -193,7 +198,7 @@ if /i [%DL_FORCE%] equ [curl] (
193198
)
194199
) else if /i [%DL_FORCE%] equ [wget] (
195200
call :check_command wget 91
196-
if [%ERRORLEVEL%] equ [0] (
201+
if [!ERRORLEVEL!] equ [0] (
197202
set DL_TOOL=wget
198203
) else (
199204
echo ERROR: /dl wget requested but 'wget' not found in PATH.
@@ -202,12 +207,12 @@ if /i [%DL_FORCE%] equ [curl] (
202207
)
203208
) else (
204209
REM Default behavior: prefer wget, else curl
205-
call :check_command wget 91
206-
if [%ERRORLEVEL%] equ [0] (
210+
call :check_command_silent wget
211+
if [!ERRORLEVEL!] equ [0] (
207212
set DL_TOOL=wget
208213
) else (
209-
call :check_command curl 91
210-
if [%ERRORLEVEL%] equ [0] (
214+
call :check_command_silent curl
215+
if [!ERRORLEVEL!] equ [0] (
211216
set DL_TOOL=curl
212217
)
213218
)

0 commit comments

Comments
 (0)