# temporalily ignore test failures
%ifarch %{ix86} x86_64
%bcond_without ignore_tests
%else
%bcond_with ignore_tests
%endif

# Define arches for PA and SA
%global golang_arches   %{ix86} x86_64
%global go_arches       %{golang_arches}

# Where to set GOPATH for builds
%global gopath          %{_datadir}/gocode

# build ids are not currently generated:
# https://code.google.com/p/go/issues/detail?id=5238
#
# also, debuginfo extraction currently fails with
# "Failed to write file: invalid section alignment"
%global debug_package %{nil}

# we are shipping the full contents of src in the data subpackage, which
# contains binary-like things (ELF data for tests, etc)
%global _binaries_in_noarch_packages_terminate_build 0

%if 0
# Do not check any files in doc or src for requires
%global __requires_exclude_from ^(%{_datadir}|/usr/lib)/%{name}/(doc|src)/.*$
%endif

# Don't alter timestamps of especially the .a files (or else go will rebuild later)
# Actually, don't strip at all since we are not even building debug packages and this corrupts the dwarf testdata
%global __strip /bin/true

# rpmbuild magic to keep from having meta dependency on libc.so.6
%global __requires_exclude_from ^.*$
%global __spec_install_post /usr/lib/rpm/check-rpaths \
  /usr/lib/rpm/check-buildroot \
  /usr/lib/rpm/brp-compress

# Golang build options.

# Buid golang using external/internal(close to cgo disabled) linking.
%global external_linker 1

# Build golang with cgo enabled/disabled(later equals more or less to internal linking).
%global cgo_enabled 1

# Use golang/gcc-go as bootstrap compiler
%global golang_bootstrap 1

# boostrap(with internal linking) using gcc-go fails due to bug in tests(https://github.com/golang/go/issues/12629)
# make check not to fail due to it

# Controls what ever we fail on failed tests
%if %{with ignore_tests}
%global fail_on_tests 0
%else
%global fail_on_tests 1
%endif

# TODO get more support for shared objects
# Build golang shared objects for stdlib
%ifarch x86_64
%global shared 1
%else
%global shared 0
%endif

# Fedora GOROOT
%global goroot          /usr/lib/%{name}

%ifarch x86_64
%global gohostarch  amd64
%endif
%ifarch %{ix86}
%global gohostarch  386
%endif

Summary:        The Go Programming Language
Summary(ja):    プログラミング言語 Go
Name:           golang
Version:        1.21.1
Release:        1%{?_dist_release}
Group:          programming
Vendor:         Project Vine
Distribution:   Vine Linux

# source tree includes several copies of Mark.Twain-Tom.Sawyer.txt under Public Domain
License:        BSD and Public Domain
URL:            https://golang.org/
# pre-processed by source.sh to make Mark.Twain-Tom.Sawyer.txt free again
Source0:        https://dl.google.com/go/go%{version}.src.tar.gz

Source100:      golang-gdbinit
Source101:      golang-prelink.conf
Source102:      macros.golang

%global go_api %(echo "%{version}" | cut -d . -f 1,2)

# These are the only RHEL/Fedora architectures that we compile this package for
ExclusiveArch:  %{golang_arches}

# The compiler is written in Go. Needs go(1.4+) compiler for build.
BuildRequires:  golang > 1.4
BuildRequires:  net-tools

# for tests
BuildRequires:  pcre-devel, glibc-static

Provides:       go = %{version}-%{release}
Requires:       %{name}-bin
Requires:       %{name}-src = %{version}-%{release}

%description
%{summary}.


%package        docs
Summary:        Golang compiler docs
Group:          documentation
Requires:       %{name} = %{version}-%{release}
#BuildArch:     noarch
Obsoletes:      %{name}-docs < 1.1-4

%description   docs
%{summary}.


%package        misc
Summary:        Golang compiler miscellaneous sources
Group:          programming
Requires:       %{name} = %{version}-%{release}
#BuildArch:     noarch

%description   misc
%{summary}.


%package        tests
Summary:        Golang compiler tests for stdlib
Group:          programming
Requires:       %{name} = %{version}-%{release}
#BuildArch:     noarch

%description   tests
%{summary}.


%package        src
Summary:        Golang compiler source tree
Group:          programming
#BuildArch:      noarch
%description    src
%{summary}


%package        bin
Summary:        Golang core compiler tools
Group:          programming
Requires:       go = %{version}-%{release}
Requires(post): %{_sbindir}/update-alternatives
Requires(postun): %{_sbindir}/update-alternatives

# We strip the meta dependency, but go does require glibc.
# This is an odd issue, still looking for a better fix.
Requires:       glibc
Requires:       gcc
%description    bin
%{summary}


# Workaround old RPM bug of symlink-replaced-with-dir failure
%pretrans -p <lua>
for _,d in pairs({"api", "doc", "include", "lib", "src"}) do
  path = "%{goroot}/" .. d
  if posix.stat(path, "type") == "link" then
    os.remove(path)
    posix.mkdir(path)
  end
end

%if %{shared}
%package        shared
Summary:        Golang shared object libraries
Group:          system

%description    shared
%{summary}.
%endif


%debug_package


%prep
%setup -q -n go


%build
# bootstrap compiler GOROOT
%if !%{golang_bootstrap}
export GOROOT_BOOTSTRAP=/
%else
export GOROOT_BOOTSTRAP=%{goroot}
%endif

# set up final install location
export GOROOT_FINAL=%{goroot}

export GOHOSTOS=linux
export GOHOSTARCH=%{gohostarch}

pushd src
# use our gcc options for this build, but store gcc as default for compiler
export CFLAGS="$RPM_OPT_FLAGS"
export LDFLAGS="$RPM_LD_FLAGS"
export CC="gcc"
export CC_FOR_TARGET="gcc"
export GOOS=linux
export GOARCH=%{gohostarch}
%if !%{external_linker}
export GO_LDFLAGS="-linkmode internal"
%endif
%if !%{cgo_enabled}
export CGO_ENABLED=0
%endif
./make.bash --no-clean
popd

# build shared std lib
%if %{shared}
GOROOT=$(pwd) PATH=$(pwd)/bin:$PATH go install -buildmode=shared std
%endif


%install
rm -rf $RPM_BUILD_ROOT

# create the top level directories
mkdir -p $RPM_BUILD_ROOT%{_bindir}
mkdir -p $RPM_BUILD_ROOT%{goroot}

# install everything into libdir (until symlink problems are fixed)
# https://code.google.com/p/go/issues/detail?id=5830
cp -apv api bin doc lib pkg src misc test VERSION \
   $RPM_BUILD_ROOT%{goroot}

# bz1099206
find $RPM_BUILD_ROOT%{goroot}/src -exec touch -r $RPM_BUILD_ROOT%{goroot}/VERSION "{}" \;
# and level out all the built archives
touch $RPM_BUILD_ROOT%{goroot}/pkg
find $RPM_BUILD_ROOT%{goroot}/pkg -exec touch -r $RPM_BUILD_ROOT%{goroot}/pkg "{}" \;
# generate the spec file ownership of this source tree and packages
cwd=$(pwd)
src_list=$cwd/go-src.list
pkg_list=$cwd/go-pkg.list
shared_list=$cwd/go-shared.list
misc_list=$cwd/go-misc.list
docs_list=$cwd/go-docs.list
tests_list=$cwd/go-tests.list
rm -f $src_list $pkg_list $docs_list $misc_list $tests_list $shared_list
touch $src_list $pkg_list $docs_list $misc_list $tests_list $shared_list
pushd $RPM_BUILD_ROOT%{goroot}
        find src/ -type d -a \( ! -name testdata -a ! -ipath '*/testdata/*' \) -printf '%%%dir %{goroot}/%p\n' >> $src_list
        find src/ ! -type d -a \( ! -ipath '*/testdata/*' -a ! -name '*_test*.go' \) -printf '%{goroot}/%p\n' >> $src_list

        find bin/ pkg/ -type d -a ! -path '*_dynlink/*' -printf '%%%dir %{goroot}/%p\n' >> $pkg_list
        find bin/ pkg/ ! -type d -a ! -path '*_dynlink/*' -printf '%{goroot}/%p\n' >> $pkg_list

        find doc/ -type d -printf '%%%dir %{goroot}/%p\n' >> $docs_list
        find doc/ ! -type d -printf '%{goroot}/%p\n' >> $docs_list

        find misc/ -type d -printf '%%%dir %{goroot}/%p\n' >> $misc_list
        find misc/ ! -type d -printf '%{goroot}/%p\n' >> $misc_list

%if %{shared}
        find pkg/*_dynlink/ -type d -printf '%%%dir %{goroot}/%p\n' >> $shared_list
        find pkg/*_dynlink/ ! -type d -printf '%{goroot}/%p\n' >> $shared_list
%endif

        find test/ -type d -printf '%%%dir %{goroot}/%p\n' >> $tests_list
        find test/ ! -type d -printf '%{goroot}/%p\n' >> $tests_list
        find src/ -type d -a \( -name testdata -o -ipath '*/testdata/*' \) -printf '%%%dir %{goroot}/%p\n' >> $tests_list
        find src/ ! -type d -a \( -ipath '*/testdata/*' -o -name '*_test*.go' \)  -printf '%{goroot}/%p\n' >> $tests_list
        # this is only the zoneinfo.zip
        find lib/ -type d -printf '%%%dir %{goroot}/%p\n' >> $tests_list
        find lib/ ! -type d -printf '%{goroot}/%p\n' >> $tests_list
popd

# remove the doc Makefile
rm -rfv $RPM_BUILD_ROOT%{goroot}/doc/Makefile

# put binaries to bindir, linked to the arch we're building,
# leave the arch independent pieces in %{goroot}
mkdir -p $RPM_BUILD_ROOT%{goroot}/bin/linux_%{gohostarch}
ln -sf %{goroot}/bin/go $RPM_BUILD_ROOT%{goroot}/bin/linux_%{gohostarch}/go
ln -sf %{goroot}/bin/gofmt $RPM_BUILD_ROOT%{goroot}/bin/linux_%{gohostarch}/gofmt

# ensure these exist and are owned
mkdir -p $RPM_BUILD_ROOT%{gopath}/src/github.com
mkdir -p $RPM_BUILD_ROOT%{gopath}/src/bitbucket.org
mkdir -p $RPM_BUILD_ROOT%{gopath}/src/code.google.com/p
mkdir -p $RPM_BUILD_ROOT%{gopath}/src/golang.org/x

# make sure these files exist and point to alternatives
rm -f $RPM_BUILD_ROOT%{_bindir}/go
ln -sf /etc/alternatives/go $RPM_BUILD_ROOT%{_bindir}/go
rm -f $RPM_BUILD_ROOT%{_bindir}/gofmt
ln -sf /etc/alternatives/gofmt $RPM_BUILD_ROOT%{_bindir}/gofmt

# gdbinit
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/gdbinit.d
cp -av %{SOURCE100} $RPM_BUILD_ROOT%{_sysconfdir}/gdbinit.d/golang.gdb

# prelink blacklist
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/prelink.conf.d
cp -av %{SOURCE101} $RPM_BUILD_ROOT%{_sysconfdir}/prelink.conf.d/golang.conf

# rpm macros
mkdir -p %{buildroot}
mkdir -p $RPM_BUILD_ROOT%{_rpmmacrodir}
cp -av %{SOURCE102} $RPM_BUILD_ROOT%{_rpmmacrodir}/


%check
export GOROOT=$(pwd -P)
export PATH="$GOROOT"/bin:"$PATH"
cd src

export CC="gcc"
export CFLAGS="$RPM_OPT_FLAGS"
export LDFLAGS="$RPM_LD_FLAGS"
%if !%{external_linker}
export GO_LDFLAGS="-linkmode internal"
%endif
%if !%{cgo_enabled} || !%{external_linker}
export CGO_ENABLED=0
%endif

# make sure to not timeout
export GO_TEST_TIMEOUT_SCALE=2

%if %{fail_on_tests}
./run.bash --no-rebuild -v -v -v -k
%else
./run.bash --no-rebuild -v -v -v -k || :
%endif
cd ..


%post bin
%{_sbindir}/update-alternatives --install %{_bindir}/go \
        go %{goroot}/bin/go 90 \
        --slave %{_bindir}/gofmt gofmt %{goroot}/bin/gofmt

%preun bin
if [ $1 = 0 ]; then
        %{_sbindir}/update-alternatives --remove go %{goroot}/bin/go
fi


%files
%license LICENSE
%doc AUTHORS CONTRIBUTORS PATENTS
# VERSION has to be present in the GOROOT, for `go install std` to work
%doc %{goroot}/VERSION
%dir %{goroot}/doc
%doc %{goroot}/doc/*

# go files
%dir %{goroot}
%exclude %{goroot}/bin/
%exclude %{goroot}/pkg/
%exclude %{goroot}/src/
%exclude %{goroot}/doc/
%exclude %{goroot}/misc/
%{goroot}/*

# ensure directory ownership, so they are cleaned up if empty
%dir %{gopath}
%dir %{gopath}/src
%dir %{gopath}/src/github.com/
%dir %{gopath}/src/bitbucket.org/
%dir %{gopath}/src/code.google.com/
%dir %{gopath}/src/code.google.com/p/
%dir %{gopath}/src/golang.org
%dir %{gopath}/src/golang.org/x


# gdbinit (for gdb debugging)
%{_sysconfdir}/gdbinit.d

# prelink blacklist
%{_sysconfdir}/prelink.conf.d

%{_rpmmacrodir}/macros.golang

%files -f go-src.list src

%files -f go-docs.list docs

%files -f go-misc.list misc

%files -f go-tests.list tests

%files -f go-pkg.list bin
%{_bindir}/go
%{_bindir}/gofmt

%if %{shared}
%files -f go-shared.list shared
%endif


%changelog
* Wed Sep 13 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.21.1-1
- new upstream release.

* Wed Aug 16 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.21.0-1
- new upstream release.

* Sat Jun 17 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.20.5-1
- new upstream release.

* Fri May 26 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.20.4-1
- new upstream release.

* Sat Apr 15 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.20.3-1
- new upstream release.

* Wed Mar 15 2023 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.20.2-1
- new upstream release.

* Sat Nov 05 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.19.3-1
- new upstream release.

* Wed Oct 19 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.19.2-1
- new upstream release.

* Sun Sep 11 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.19.1-1
- new upstream release.

* Tue Aug 16 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.19-1
- new upstream release.

* Thu Jul 14 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.18.4-1
- new upstream release.

* Mon May 02 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.18.1-1
- new upstream release.

* Thu Feb 17 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.17.7-1
- new upstream release.

* Thu Jan 13 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.17.6-1
- new upstream release.

* Thu Nov 11 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.17.3-1
- new upstream release.

* Fri Oct 22 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.17.2-1
- new upstream release.

* Wed Aug 18 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.17-1
- new upstream release.

* Sat Jul 31 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.16.6-1
- new upstream release.

* Tue Jun 08 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.16.5-1
- new upstream release.

* Fri Mar 19 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.16.2-1
- new upstream release.

* Wed Feb 03 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.15.7-1
- new upstream release.

* Wed Dec 16 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.15.6-1
- new upstream release.

* Thu Nov 19 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.15.5-1
- new upstream release.

* Fri Sep 04 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.15.1-1
- new upstream release.

* Wed Aug 19 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.15-1
- new upstream release.

* Sat Jul 25 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.14.6-1
- new upstream release.

* Sat Mar 21 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.14.1-1
- new upstream release.

* Sun Dec 22 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.13.5-1
- new upstream release.

* Sat Nov 02 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.13.4-1
- new upstream release.

* Tue Jun 26 2018 IWAI, Masaharu <iwaim.sub@gmail.com> 1.10.3-1
- update to 1.10.3
- drop patches (Patch0, 212, 215)
- export 'GO_TEST_TIMEOUT_SCALE=2' in %%check
- add bcond for tests

* Tue Jun 06 2017 IWAI, Masaharu <iwaim.sub@gmail.com> 1.8.3-1
- update to 1.8.3

* Mon Jun  5 2017 IWAI, Masaharu <iwaim.sub@gmail.com> 1.8.1-1
- update to 1.8.1
- remove BuildArch tags
- drop Source1
- drop patches
 - remove ECC p224 patch (Patch1)
 - X.509 patch (Patch2): upstream merged
 - disable TestGdbPython patch (Patch213)
 - disable TestCloneNEWUSERAndRemapNoRootDisableSetgroups patch (Patch214)
 - drop Patch216: upstream fixed
- update bootstrap binary path patch (Patch212)

* Mon Dec 21 2015 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.5.2-1
- new upstream release.

* Sun Mar  8 2015 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 1.4.2-1
- new upstream release.

* Mon Dec 15 2014 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 1.2-2
- added Group tag

* Sat Feb  1 2014 IWAI, Masaharu <iwaim.sub@gmail.com> 1.2
- initial build for Vine Linux
- drop emacs and vim package

* Fri Jan 24 2014 Vincent Batts <vbatts@redhat.com> 1.2-4
- skip a flaky test that is sporadically failing on the build server

* Thu Jan 16 2014 Vincent Batts <vbatts@redhat.com> 1.2-3
- remove golang-godoc dependency. cyclic dependency on compiling godoc

* Wed Dec 18 2013 Vincent Batts <vbatts@redhat.com> - 1.2-2
- removing P224 ECC curve

* Mon Dec 2 2013 Vincent Batts <vbatts@fedoraproject.org> - 1.2-1
- Update to upstream 1.2 release
- remove the pax tar patches

* Tue Nov 26 2013 Vincent Batts <vbatts@redhat.com> - 1.1.2-8
- fix the rpmspec conditional for rhel and fedora

* Thu Nov 21 2013 Vincent Batts <vbatts@redhat.com> - 1.1.2-7
- patch tests for testing on rawhide
- let the same spec work for rhel and fedora

* Wed Nov 20 2013 Vincent Batts <vbatts@redhat.com> - 1.1.2-6
- don't symlink /usr/bin out to ../lib..., move the file
- seperate out godoc, to accomodate the go.tools godoc

* Fri Sep 20 2013 Adam Miller <maxamillion@fedoraproject.org> - 1.1.2-5
- Pull upstream patches for BZ#1010271
- Add glibc requirement that got dropped because of meta dep fix

* Fri Aug 30 2013 Adam Miller <maxamillion@fedoraproject.org> - 1.1.2-4
- fix the libc meta dependency (thanks to vbatts [at] redhat.com for the fix)

* Tue Aug 27 2013 Adam Miller <maxamillion@fedoraproject.org> - 1.1.2-3
- Revert incorrect merged changelog

* Tue Aug 27 2013 Adam Miller <maxamillion@fedoraproject.org> - 1.1.2-2
- This was reverted, just a placeholder changelog entry for bad merge

* Tue Aug 20 2013 Adam Miller <maxamillion@fedoraproject.org> - 1.1.2-1
- Update to latest upstream

* Sat Aug 03 2013 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 1.1.1-7
- Rebuilt for https://fedoraproject.org/wiki/Fedora_20_Mass_Rebuild

* Wed Jul 17 2013 Petr Pisar <ppisar@redhat.com> - 1.1.1-6
- Perl 5.18 rebuild

* Wed Jul 10 2013 Adam Goode <adam@spicenitz.org> - 1.1.1-5
- Blacklist testdata files from prelink
- Again try to fix #973842

* Fri Jul  5 2013 Adam Goode <adam@spicenitz.org> - 1.1.1-4
- Move src to libdir for now (#973842) (upstream issue https://code.google.com/p/go/issues/detail?id=5830)
- Eliminate noarch data package to work around RPM bug (#975909)
- Try to add runtime-gdb.py to the gdb safe-path (#981356)

* Wed Jun 19 2013 Adam Goode <adam@spicenitz.org> - 1.1.1-3
- Use lua for pretrans (http://fedoraproject.org/wiki/Packaging:Guidelines#The_.25pretrans_scriptlet)

* Mon Jun 17 2013 Adam Goode <adam@spicenitz.org> - 1.1.1-2
- Hopefully really fix #973842
- Fix update from pre-1.1.1 (#974840)

* Thu Jun 13 2013 Adam Goode <adam@spicenitz.org> - 1.1.1-1
- Update to 1.1.1
- Fix basically useless package (#973842)

* Sat May 25 2013 Dan Horák <dan[at]danny.cz> - 1.1-3
- set ExclusiveArch

* Fri May 24 2013 Adam Goode <adam@spicenitz.org> - 1.1-2
- Fix noarch package discrepancies

* Fri May 24 2013 Adam Goode <adam@spicenitz.org> - 1.1-1
- Initial Fedora release.
- Update to 1.1

* Thu May  9 2013 Adam Goode <adam@spicenitz.org> - 1.1-0.3.rc3
- Update to rc3

* Thu Apr 11 2013 Adam Goode <adam@spicenitz.org> - 1.1-0.2.beta2
- Update to beta2

* Tue Apr  9 2013 Adam Goode <adam@spicenitz.org> - 1.1-0.1.beta1
- Initial packaging.