Skip to content

Commit 64a4a4b

Browse files
committed
Fix Go version discovery on Ubuntu versions
1 parent 0e42142 commit 64a4a4b

1 file changed

Lines changed: 61 additions & 10 deletions

File tree

uyuni-tools.spec

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ BuildRequires: golang(API) >= 1.25
8282
# suse_version
8383

8484
%if 0%{?ubuntu}
85-
%if 0%{?ubuntu} >= 2404
86-
# Will use the default golang of the OS which is higher than minimal required go_version
87-
BuildRequires: golang
88-
%else
89-
# Overwrite with new go version the OS default
9085
%define go_version 1.22
91-
BuildRequires: golang-%{go_version}
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
91+
BuildRequires: golang >= %{go_version}
9292
%endif
9393
%endif
9494
# ubuntu
@@ -330,15 +330,66 @@ go_tags=""
330330

331331
go_path=""
332332
%if 0%{?ubuntu}
333-
if test -d /usr/lib/go-%{go_version}/bin/; then
334-
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
335388
fi
336389
%else
337390
%if "%{?_go_bin}" != ""
338391
go_path='%{_go_bin}/'
339392
%endif
340-
# "_go_bin" != ""
341-
342393
%endif
343394
# ubuntu
344395

0 commit comments

Comments
 (0)