@@ -56,6 +56,8 @@ STREAM_INSTALL_PACKAGE=""
5656STREAM_PRESENT=0
5757STREAM_BINARY=" stream"
5858STREAM_BUILD_MESSAGE=" "
59+ STREAM_CC=" "
60+ STREAM_FETCHER=" "
5961
6062# -----------------------------------------------------------------------------
6163# CLI flags
@@ -186,10 +188,12 @@ pkg_install() {
186188}
187189
188190ensure_c_compiler_for_stream () {
191+ STREAM_CC=" "
192+
189193 local cc
190194 cc=" $( command -v cc || command -v gcc || command -v clang || true) "
191195 if [[ -n " $cc " ]]; then
192- echo " $cc "
196+ STREAM_CC= " $cc "
193197 return 0
194198 fi
195199
@@ -236,64 +240,146 @@ ensure_c_compiler_for_stream() {
236240 return 1
237241 fi
238242
239- echo " $cc "
243+ STREAM_CC=" $cc "
244+ return 0
245+ }
246+
247+ ensure_fetcher_for_stream () {
248+ STREAM_FETCHER=" "
249+
250+ if have curl; then
251+ STREAM_FETCHER=" curl"
252+ return 0
253+ fi
254+ if have wget; then
255+ STREAM_FETCHER=" wget"
256+ return 0
257+ fi
258+
259+ [[ " ${AUTO_INSTALL} " -eq 1 ]] || { STREAM_BUILD_MESSAGE=" Cannot fetch STREAM source: need curl or wget and auto-install is disabled (--no-install or AUTO_INSTALL=0)." ; return 1; }
260+ if ! need_root_or_sudo; then
261+ STREAM_BUILD_MESSAGE=" Cannot fetch STREAM source: curl/wget missing and sudo/root is not available non-interactively."
262+ return 1
263+ fi
264+
265+ local SUDO; SUDO=" $( sudo_prefix) "
266+ local pm=" "
267+ local install_cmd=()
268+
269+ if have apt-get; then
270+ pm=" apt-get"
271+ note " Installing curl/wget for STREAM fetch via ${pm} "
272+ cmd ${SUDO} apt-get update -y
273+ install_cmd=(${SUDO} apt-get install -y curl wget)
274+ elif have dnf; then
275+ pm=" dnf"
276+ note " Installing curl/wget for STREAM fetch via ${pm} "
277+ install_cmd=(${SUDO} dnf install -y curl wget)
278+ elif have yum; then
279+ pm=" yum"
280+ note " Installing curl/wget for STREAM fetch via ${pm} "
281+ install_cmd=(${SUDO} yum install -y curl wget)
282+ elif have apk; then
283+ pm=" apk"
284+ note " Installing curl/wget for STREAM fetch via ${pm} "
285+ install_cmd=(${SUDO} apk add --no-cache curl wget)
286+ else
287+ STREAM_BUILD_MESSAGE=" Cannot fetch STREAM source: no supported package manager to install curl/wget (need apt-get/dnf/yum/apk)."
288+ return 1
289+ fi
290+
291+ if ! cmd " ${install_cmd[@]} " ; then
292+ local exit_code=$?
293+ STREAM_BUILD_MESSAGE=" Failed to install curl/wget via ${pm} (exit ${exit_code} )."
294+ return 1
295+ fi
296+
297+ hash -r || true
298+ if have curl; then
299+ STREAM_FETCHER=" curl"
300+ return 0
301+ fi
302+ if have wget; then
303+ STREAM_FETCHER=" wget"
304+ return 0
305+ fi
306+
307+ STREAM_BUILD_MESSAGE=" Cannot fetch STREAM source: curl/wget still unavailable after install attempt."
308+ return 1
240309}
241310
242311build_stream_from_source () {
243312 STREAM_BUILD_MESSAGE=" "
244313
245- local cc
246- cc=" $( ensure_c_compiler_for_stream) " || return 1
314+ if ! ensure_c_compiler_for_stream; then
315+ return 1
316+ fi
317+ local cc=" $STREAM_CC "
318+ if ! ensure_fetcher_for_stream; then
319+ return 1
320+ fi
321+ local fetcher=" $STREAM_FETCHER "
247322
248323 local build_dir
249324 build_dir=" $( mktemp -d /tmp/node_bench_stream_XXXXXX 2> /dev/null || true) "
250325 [[ -n " $build_dir " ]] || { STREAM_BUILD_MESSAGE=" Failed to create temporary directory for STREAM build." ; return 1; }
326+ trap ' rm -rf "$build_dir" >/dev/null 2>&1' RETURN
251327
252328 local src=" ${build_dir} /stream.c"
253329 local url=" https://www.cs.virginia.edu/stream/FTP/Code/stream.c"
254330
255- if have curl; then
331+ if [[ " $fetcher " == " curl" ]] ; then
256332 note " Fetching STREAM source from ${url} "
257333 if ! curl -fsSL " $url " -o " $src " ; then
258334 STREAM_BUILD_MESSAGE=" Failed to download STREAM source from ${url} "
259335 return 1
260336 fi
261- elif have wget ; then
337+ else
262338 note " Fetching STREAM source from ${url} (via wget)"
263339 if ! wget -qO " $src " " $url " ; then
264340 STREAM_BUILD_MESSAGE=" Failed to download STREAM source from ${url} "
265341 return 1
266342 fi
267- else
268- STREAM_BUILD_MESSAGE=" Cannot fetch STREAM source: need curl or wget."
269- return 1
270343 fi
271344
272345 local target=" ${build_dir} /stream"
273346 local compile_log=" ${build_dir} /compile.log"
347+ local build_variant=" "
274348
275349 note " Building STREAM from source using ${cc} (attempting OpenMP, falling back if unavailable)"
276350 if " $cc " -O3 -fopenmp " $src " -o " $target " > " $compile_log " 2>&1 ; then
277351 chmod +x " $target " || true
278- PATH=" $( dirname " $target " ) :$PATH "
279- STREAM_BINARY=" $target "
280- STREAM_BUILD_MESSAGE=" Built STREAM from source using ${cc} with OpenMP (path: ${target} )"
281- return 0
352+ build_variant=" with OpenMP"
353+ else
354+ note " Retrying STREAM build without OpenMP flags"
355+ if " $cc " -O3 " $src " -o " $target " > " $compile_log " 2>&1 ; then
356+ chmod +x " $target " || true
357+ build_variant=" without OpenMP"
358+ else
359+ local tail_log
360+ tail_log=" $( tail -n 20 " $compile_log " 2> /dev/null || true) "
361+ STREAM_BUILD_MESSAGE=" Failed to build STREAM from source with ${cc}${tail_log: +; tail of build log: \n ${tail_log} } "
362+ return 1
363+ fi
282364 fi
283365
284- note " Retrying STREAM build without OpenMP flags"
285- if " $cc " -O3 " $src " -o " $target " > " $compile_log " 2>&1 ; then
286- chmod +x " $target " || true
287- PATH=" $( dirname " $target " ) :$PATH "
288- STREAM_BINARY=" $target "
289- STREAM_BUILD_MESSAGE=" Built STREAM from source using ${cc} (no OpenMP) (path: ${target} )"
290- return 0
366+ local install_target=" /usr/local/bin/stream"
367+ local SUDO; SUDO=" $( sudo_prefix) "
368+ if ! need_root_or_sudo; then
369+ STREAM_BUILD_MESSAGE=" Built STREAM binary (${build_variant} ) but cannot install to ${install_target} : sudo/root not available non-interactively."
370+ return 1
291371 fi
292372
293- local tail_log
294- tail_log=" $( tail -n 20 " $compile_log " 2> /dev/null || true) "
295- STREAM_BUILD_MESSAGE=" Failed to build STREAM from source with ${cc}${tail_log: +; tail of build log: \n ${tail_log} } "
296- return 1
373+ note " Installing STREAM binary to ${install_target} "
374+ if ! cmd ${SUDO} install -m 0755 " $target " " $install_target " ; then
375+ STREAM_BUILD_MESSAGE=" Built STREAM binary (${build_variant} ) but failed to install to ${install_target} ."
376+ return 1
377+ fi
378+
379+ hash -r || true
380+ STREAM_BINARY=" $install_target "
381+ STREAM_BUILD_MESSAGE=" Built STREAM from source using ${cc} (${build_variant} ) and installed to ${install_target} "
382+ return 0
297383}
298384
299385install_stream_with_fallback () {
0 commit comments