File go.spec of Package go

%define realname go
%define mainver  1.15
%define realver  %{mainver}
%define extraver .src
%define srcext   tar.gz

%bcond_without go

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

# Params for update-alternatives
%define alt_prio %(tr -d . <<< %{mainver})

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

# Install-time parameters
Provides:      golang%{mainver} = %{version}-%{release}
Provides:      golang(API) = %{mainver}
%if 0%{?suse_version}
Requires(post): alternatives
%else
Requires(post): chkconfig
%endif

# Build-time parameters
BuildRequires: gcc-c++
BuildRequires: binutils-gold
%if %{with go}
BuildRequires: golang >= 1.4
%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: glibc-devel-static
BuildRequires: netcfg
%endif
%if 0%{?rhel}
BuildRequires: glibc-static
BuildRequires: setup
%endif
BuildRoot:     %{_tmppath}/%{name}-root
Source0:       https://storage.googleapis.com/golang/%{realname}%{realver}%{?extraver}.%{srcext}
Source1:       compiler-rt-latest.tar.bz2

%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}
Provides:      golang = %{version}-%{release}
Obsoletes:     golang < %{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} -a1
find . -type f -name \*.syso -delete
# Fix shebang
%{__sed} -i '1 s|^.*$|#!/bin/bash|' src/all.bash

%build
export CGO_ENABLED=1
export CGO_CFLAGS='%{optflags} -g -UNDEBUG'
export CGO_LDFLAGS='-Wl,--as-needed -Wl,-z,now -Wl,-z,relro'
export PKG_CONFIG=%{_bindir}/pkg-config
export GOCACHE=$(pwd -P)/.gocache
export GOTRACEBACK=crash
export GO_GCFLAGS='-v'
export GOROOT_FINAL=%{_libdir}/go
%if %{with go}
export GOOS=$(go env GOOS)
export GOARCH=$(go env GOARCH)
export GOROOT_BOOTSTRAP=$(go env GOROOT)
%else
export GOROOT_BOOTSTRAP=%{_prefix}
export GOGCCFLAGS='-O2 -fuse-ld=gold'
%endif
cd src
bash -xe ./make.bash -v -no-banner
cd ..
# 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
%ifarch x86_64
(
  cd compiler-rt-latest/lib/tsan/go
  ./buildgo.sh
)
cp -v compiler-rt-latest/lib/tsan/go/race_${GOOS}_${GOARCH}.syso src/runtime/race/
bin/go install -v -race std
%endif

%install
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
%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
# 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
bin/go tool dist test --no-rebuild || :

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

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

%files -n go
%defattr(-,root,root)
%doc AUTHORS CONTRIBUTORS LICENSE 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