File go.spec of Package go

%define realname go
%define realver  1.25.0
%define extraver .src
%define srcext   tar.gz

%define mainver  %(awk -F. '{printf "%i.%i", $1, $2}' <<< %{realver})
%define alt_prio %(awk -F. '{printf "%i%02i", $1, $2}' <<< %{realver})

%bcond_without go

# turn off the generation of debuginfo rpm  (RH9) ??
%global debug_package %{nil}

# Don't strip binaries/libraries
%global __strip /bin/true

# Common info
Name:          %{realname}%{mainver}
Version:       %{realver}
Release:       %{?extraver:0.}1%{?dist}
License:       BSD-3-Clause
Group:         Development/Languages/Other
URL:           https://go.dev/
Summary:       The Go Programming Language

# Install-time parameters
Provides:      golang%{mainver} = %{version}-%{release}
Provides:      golang(API) = %{mainver}

# Build-time parameters
BuildRequires: gcc-c++
BuildRequires: pkgconfig
%if %{with go}
BuildRequires: go
%else
BuildRequires: gcc-go >= 5.0
%endif
%if 0%{?suse_version}
BuildRequires: fdupes
%endif
# Tests
# Static libc is required to test static binaries building
%if 0%{?suse_version}
BuildRequires: netcfg
%endif
%if 0%{?rhel}
BuildRequires: setup
%endif
# Tests
### BuildRequires: glibc-static
BuildRoot:     %{_tmppath}/%{name}-root
Source0:       https://storage.googleapis.com/golang/%{realname}%{realver}%{?extraver}.%{srcext}

%description
Go is an open source programming language that makes it easy to build simple,
reliable, and efficient software.

%package -n go
Group:         Development/Languages/Other
Summary:       The Go Programming Language

Requires:      %{name} = %{version}-%{release}
Requires(post): alternatives

Provides:      golang = %{version}-%{release}
Provides:      go-toolset = %{version}-%{release}
Obsoletes:     golang < %{version}-%{release}
Obsoletes:     go-toolset < %{version}-%{release}

%description -n go
Go is an open source programming language that makes it easy to build simple,
reliable, and efficient software.

# Preparation step (unpackung and patching if necessary)
%prep
%setup -q -n %{realname}
# Fix shebang
%{__sed} -ri '1 s|^#!/usr/bin/env\s+(.+)$|#!/bin/\1|' src/*.bash
# Fix pkg-config test
%{__sed} -ri '/#cgo/ s/(pkg-config:)/\1 --print-errors/' src/cmd/go/testdata/script/list_pkgconfig_error.txt

%build
export CGO_ENABLED=1
export CGO_CFLAGS='%{optflags}'
export CGO_CXXFLAGS='%{optflags}'
export CGO_LDFLAGS='-Wl,--as-needed -Wl,--strip-all -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro'
export GOFLAGS='-v -a -gcflags=-dwarf=false -ldflags=-s'
export GOCACHE=$(pwd -P)/.gocache
export GOTRACEBACK=crash
export GOROOT_FINAL=%{_libdir}/go
( cd src; bash -xe ./make.bash -v )
# Prepare file with environment variables for just built Go
GOROOT=$(pwd -P) bin/go env > go_env.sh
source ./go_env.sh
bin/go install -v -buildmode=shared -linkshared std

%install
export NO_BRP_AR=true
export NO_BRP_STRIP_DEBUG=true
source ./go_env.sh
%{__install} -d -m0755 %{buildroot}%{_sysconfdir}/rpm
%{__install} -d -m0755 %{buildroot}%{_bindir}
%{__install} -d -m0755 %{buildroot}%{_libdir}/go/%{mainver}/{bin,pkg,src}
%{__install} -d -m0755 %{buildroot}%{_datadir}/go/%{mainver}/contrib/{bin,pkg,src}
%{__install} -d -m0755 %{buildroot}%{_datadir}/go/%{mainver}/contrib/pkg/${GOOS}_${GOARCH}
%{__install} -m0755 bin/*        %{buildroot}%{_libdir}/go/%{mainver}/bin/
%{__install} -m0755 src/all.bash %{buildroot}%{_libdir}/go/%{mainver}/src/
# Install package contents excluding bootstrap
find pkg -type f ! -path '*/bootstrap/*' -a ! -path '*/obj/*' | while read file; do
    %{__install} -D -m644 ${file} %{buildroot}%{_libdir}/go/%{mainver}/${file}
done
# Make tools executable
chmod a+x %{buildroot}%{_libdir}/go/%{mainver}/pkg/tool/${GOOS}_${GOARCH}/*
# Install module sources
find src -mindepth 2 -type f ! -path '*/testdata*' | while read file; do
    %{__install} -D -m644 ${file} %{buildroot}%{_libdir}/go/%{mainver}/${file}
done
# Install go.env
%{__install} -m0644 go.env %{buildroot}%{_libdir}/go/%{mainver}/
# (SUSE-specific) Remove file duplicates
%if 0%{?suse_version}
%fdupes %{buildroot}%{_libdir}/go/%{mainver}/
%endif
# Create RPM macros file
cat > %{buildroot}%{_sysconfdir}/rpm/macros.go <<EOF
# Go specific macros

%%go_ver         %{version}
%%go_build_ver   %%(go version | awk '{print \$3}')
%%go_arch        ${GOARCH}
%%go_os          ${GOOS}

%%go_root        %%{_libdir}/go/%{mainver}
%%go_path        %%{_datadir}/go/%{mainver}/contrib

%%go_dir            %%{_libdir}/go/%{mainver}
%%go_sitedir        %%{_libdir}/go/%{mainver}/pkg
%%go_sitearch       %%{_libdir}/go/%{mainver}/pkg/%%{go_os}_%%{go_arch}
%%go_contribdir     %%{_libdir}/go/%{mainver}/contrib/pkg/%%{go_os}_%%{go_arch}
%%go_contribsrcdir  %%{_datadir}/go/%{mainver}/contrib/src/
%%go_tooldir        %%{_datadir}/go/%{mainver}/pkg/tool/%%{go_os}_%%{go_arch}
EOF
# Strip binaries
for bin in %{buildroot}%{_libdir}/go/%{mainver}/bin/* %{buildroot}%{_libdir}/go/%{mainver}/pkg/tool/${GOOS}_${GOARCH}/*; do
    strip --strip-unneeded $bin
done
# Prepare alternatives
%{__install} -d -m755 %{buildroot}%{_sysconfdir}/alternatives/
touch %{buildroot}%{_sysconfdir}/alternatives/{go,gofmt}
%{__ln_s} -f %{_sysconfdir}/alternatives/{go,gofmt} %{buildroot}%{_bindir}/
# Cleanup
find %{buildroot}%{_libdir}/go/%{mainver}/ -name .gitignore -delete

%check
source ./go_env.sh
unset CGO_ENABLED
bin/go tool dist test -k --no-rebuild -v || :

%clean
[ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}

%files
%defattr(-,root,root)
%license LICENSE
%doc PATENTS README.md
%dir %{_libdir}/go
%dir %{_datadir}/go
%{_libdir}/go/%{mainver}/
%{_datadir}/go/%{mainver}/

%files -n go
%defattr(-,root,root)
%license LICENSE
%doc PATENTS README.md
%config %{_sysconfdir}/rpm/macros.go
%{_bindir}/go*
%ghost %{_sysconfdir}/alternatives/go*

%post -n go
if ! update-alternatives --list go | grep -q %{_libdir}/go/%{mainver}/bin/go; then
  update-alternatives \
    --install /usr/bin/go    go    %{_libdir}/go/%{mainver}/bin/go    %{alt_prio} \
    --slave   /usr/bin/gofmt gofmt %{_libdir}/go/%{mainver}/bin/gofmt
fi

%postun -n go
if [ ! -x %{_libdir}/go/%{mainver}/bin/go ]; then
  update-alternatives --remove go %{_libdir}/go/%{mainver}/bin/go
fi
update-alternatives --auto go || :

%changelog
openSUSE Build Service is sponsored by