Skip to content

Commit 7177372

Browse files
committed
Fix Go version discovery on Ubuntu versions
1 parent 0ac8612 commit 7177372

1 file changed

Lines changed: 61 additions & 4 deletions

File tree

uyuni-tools.spec

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,14 @@ BuildRequires: golang(API) >= 1.25
8383

8484
%if 0%{?ubuntu}
8585
%define go_version 1.22
86+
# On Ubuntu 22.04, the metapackage golang is too old (1.18) and
87+
# we must use the versioned package name specifically for this OS version.
88+
%if 0%{?ubuntu} == 2204
89+
BuildRequires: golang-%{go_version}-go
90+
%else
8691
BuildRequires: golang >= %{go_version}
8792
%endif
93+
%endif
8894
# ubuntu
8995

9096
%if 0%{?debian}
@@ -324,15 +330,66 @@ go_tags=""
324330

325331
go_path=""
326332
%if 0%{?ubuntu}
327-
if test -d /usr/lib/go-%{go_version}/bin/; then
328-
go_path=/usr/lib/go-%{go_version}/bin/
333+
max_major=0
334+
max_minor=0
335+
max_patch=0
336+
max_ver_str="none"
337+
# Loop through all versioned paths and the system path to find the absolute maximum
338+
for check_bin in /usr/lib/go-*/bin/go /usr/bin/go; do
339+
if [ -x "$check_bin" ]; then
340+
# Query the binary for its version (e.g., go1.22.1)
341+
raw_v=$("$check_bin" env GOVERSION 2>/dev/null)
342+
v_str="${raw_v#go}"
343+
# Split into integers
344+
cur_major="${v_str%%.*}"
345+
cur_minor_full="${v_str#*.}"
346+
cur_minor="${cur_minor_full%%.*}"
347+
348+
cur_patch=0
349+
if [ "$cur_minor_full" != "$cur_minor" ]; then
350+
cur_patch_full="${cur_minor_full#*.}"
351+
cur_patch="${cur_patch_full%%.*}"
352+
fi
353+
354+
# Comparison logic across found versions
355+
is_newer=0
356+
if [ "$cur_major" -gt "$max_major" ]; then
357+
is_newer=1
358+
elif [ "$cur_major" -eq "$max_major" ]; then
359+
if [ "$cur_minor" -gt "$max_minor" ]; then
360+
is_newer=1
361+
elif [ "$cur_minor" -eq "$max_minor" ] && [ "$cur_patch" -gt "$max_patch" ]; then
362+
is_newer=1
363+
fi
364+
fi
365+
366+
if [ "$is_newer" -eq 1 ]; then
367+
max_major=$cur_major
368+
max_minor=$cur_minor
369+
max_patch=$cur_patch
370+
max_ver_str="$v_str"
371+
go_path="${check_bin%go}"
372+
fi
373+
fi
374+
done
375+
376+
# Check that the Go compiler we found is new enough
377+
req_ver="%{go_version}"
378+
req_major="${req_ver%%.*}"
379+
req_minor_full="${req_ver#*.}"
380+
req_minor="${req_minor_full%%.*}"
381+
if [ "$max_ver_str" = "none" ]; then
382+
echo "ERROR: No Go compiler found in /usr/lib/go-*/bin or /usr/bin/go"
383+
exit 1
384+
fi
385+
if [ "$max_major" -lt "$req_major" ] || { [ "$max_major" -eq "$req_major" ] && [ "$max_minor" -lt "$req_minor" ]; }; then
386+
echo "ERROR: Highest found Go version is ${max_ver_str}, but version >= %{go_version} is required."
387+
exit 1
329388
fi
330389
%else
331390
%if "%{?_go_bin}" != ""
332391
go_path='%{_go_bin}/'
333392
%endif
334-
# "_go_bin" != ""
335-
336393
%endif
337394
# ubuntu
338395

0 commit comments

Comments
 (0)