# Copyright © 2015-2019 Jakub Cajka <jcajka@redhat.com>,
#                       Jan Chaloupka <jchaloup@redhat.com>,
#                       Nicolas Mailhot <nim@fedoraproject.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# This file contains macros for building projects in golang for packages
# with golang compiler or gcc-go compiler based on an architecture.
# Golang is primarly for primary architectures, gcc-go for secondary.
#
# This file provides only macros and must not use any other package except
# redhat-rpm-macros.
#
# SPDX-License-Identifier: GPL-3.0-or-later

# Define arches for PA and SA

# There is no %%go_arches_future that contains %%gccgo_arches, as
# %%gccgo_arches don't match any currently supported Fedora architectures
%golang_arches_future x86_64 armv3l armv4b armv4l armv4tl armv5tl armv5tel armv5tejl armv6l armv6hl armv7l armv7hl armv7hnl armv8l armv8hl armv8hnl armv8hcnl aarch64 ppc64le s390x
%golang_arches   x86_64 aarch64 ppc64le s390x
%gccgo_arches    mips mipsel mipsr6 mipsr6el mips64 mips64el mips64r6 mips64r6el
%go_arches       %{golang_arches} %{gccgo_arches}

# Where to set GOPATH for builds
%gopath          /usr/share/gocode

# Define go_compilers macro to signal go-compiler package is available
%go_compiler     1

# Sanitize a Go import path that can then serve as rpm package name
# Mandatory parameter: a Go import path (without flag)
# Optional parameters:
#  – “-c <compatid>” a compat identifier, used to distinguish the package
#                    from the primary package tracking the project recommended
#                    version. For example: a (different) major version, a
#                    specific shortened commit, etc
%gorpmname(c:) %{lua:
local go = require "fedora.srpm.go"
print(go.rpmname("%1", "%{-c*}"))
}

# Map Go information to rpm metadata. This macro will compute default spec
# variable values.
#
# The following spec variable MUST be set before calling the macro:
#
#   goipath   the packaged Go project import path
#
# The following spec variables SHOULD be set before calling the macro:
#
#   forgeurl  the project url on the forge,
#             if it can not be deduced from goipath
#   Version   if applicable, set it with Version: <version>
#   tag       if applicable
#   commit    if applicable
#   date      if applicable (to override the mtime of the Source archive)
#
#  Use -z for multiple calls to the macro
#
# The macro will attempt to compute and set the following variables if they are
# not already set by the packager:
#
#   goname         an rpm-compatible package name derived from goipath
#   gosource       an URL that can be used as SourceX: value
#   gourl          an URL that can be used as URL: value
#
# It will delegate processing to the forgemeta macro for:
#
#   forgesource    an URL that can be used as SourceX: value
#   forgesetupargs the correct arguments to pass to %setup for this source
#                  used by %forgesetup and %forgeautosetup
#   archivename    the source archive filename, without extentions
#   archiveext     the source archive filename extensions, without leading dot
#   archiveurl     the url that can be used to download the source archive,
#                  without renaming
#   topdir         the source archive top directory (can be empty)
#   extractdir     the source directory created inside %{_builddir} after using
#                  %%forgesetup, %forgeautosetup or %{forgesetupargs}
#   repo           the repository name
#   owner          the repository owner (if used by another computed variable)
#   shortcommit    the commit hash clamping used by the forge, if any
#   scm            the scm type, when packaging code snapshots: commits or tags
#   distprefix     the prefix that needs adding to dist to trace non-release packaging
#
# Most of the computed variables are both overridable and optional.
#
# Optional parameters:
#   -a          process all sources in one go, instead of using separate -z calls
#   -z <number> suffix all the read and written variable names with <number>
#               for example read     goipath<number>, version<number>…
#                       and generate goname<number>, archiveurl<number>…
#               The macro assumes that null or nil suffix is used for the primary
#               package source.
#   -s  Silently ignore problems in forgeurl, use it if it can be parsed,
#       ignore it otherwise.
#   -v  Be verbose and print every spec variable the macro sets.
#   -i  Print some info about the state of spec variables the macro may use or
#       set at the end of the processing.
#   -f  Use ExclusiveArch: %%{golang_arches_future}, which excludes the package
#       from %ix86. All new go packages should use this option.
%gometa(az:svif) %{lua:
if rpm.expand("%{-f}") == "" then
  exclusive_arches = "%{golang_arches}"
else
  exclusive_arches = "%{golang_arches_future}"
end
print(           "BuildRequires: go-rpm-macros\\n")
print(rpm.expand("ExclusiveArch: " .. exclusive_arches .. "\\n"))
local      fedora =  require "fedora.common"
local          go =  require "fedora.srpm.go"
local     verbose =  rpm.expand("%{-v}") ~= ""
local informative =  rpm.expand("%{-i}") ~= ""
local      silent =  rpm.expand("%{-s}") ~= ""
local  processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "")
if processall then
  for _,s in pairs(fedora.getsuffixes("goipath")) do
    go.meta(s,verbose,informative,silent)
  end
else
  go.meta(rpm.expand("%{-z*}"),verbose,informative,silent)
end
}

# Create %package sections for every known kind of go subpackage. Arguments:
# -v                  be verbose
%gopkg(av) %{expand:
%godevelpkg -a %{-v}
%goaltpkg   -a %{-v}
}

# Create a %package section for a golang-*-devel subpackage. Arguments:
# -z <number>         read the zth block of definitions, for example
#                     %{goipaths<number>}
# -a                  process all blocks in one go, instead of using separate
#                     -z calls
# -v                  be verbose
%godevelpkg(z:av) %{lua:
local         go =  require "fedora.srpm.go"
local     suffix =  rpm.expand("%{-z*}")
local processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "")
local    verbose = (rpm.expand("%{-v}") ~= "")
go.pkg("devel", suffix, processall, verbose)
}

# Create a %package section for a compat-golang-*-devel subpackage. Arguments:
# -z <number>         read the zth block of definitions, for example
#                     %{goaltipaths<number>}
# -a                  process all blocks in one go, instead of using separate
#                     -z calls
# -v                  be verbose
%goaltpkg(z:av) %{lua:
local         go =  require "fedora.srpm.go"
local     suffix =  rpm.expand("%{-z*}")
local processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "")
local    verbose = (rpm.expand("%{-v}") ~= "")
go.pkg("alt", suffix, processall, verbose)
}

# Create %files sections for every known kind of Go subpackage. Arguments:
# -v                  be verbose
%gopkgfiles(av) %{expand:
%godevelfiles -a %{-v}
%goaltfiles   -a %{-v}
}

# Create a %files section for a golang-*-devel subpackage. Arguments:
# -z <number>         read the zth block of definitions, for example
#                     %{goipaths<number>}
# -a                  process all blocks in one go, instead of using separate
#                     -z calls
# -v                  be verbose
%godevelfiles(z:av) %{lua:
local         go =  require "fedora.srpm.go"
local     suffix =  rpm.expand("%{-z*}")
local processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "")
local    verbose = (rpm.expand("%{-v}") ~= "")
go.files("devel", suffix, processall, verbose)
}

# Create a %files section for a compat-golang-*-devel subpackage. Arguments:
# -z <number>         read the zth block of definitions, for example
#                     %{goaltipaths<number>}
# -a                  process all blocks in one go, instead of using separate
#                     -z calls
# -v                  be verbose
%goaltfiles(z:av) %{lua:
local         go =  require "fedora.srpm.go"
local     suffix =  rpm.expand("%{-z*}")
local processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "")
local    verbose = (rpm.expand("%{-v}") ~= "")
go.files("alt", suffix, processall, verbose)
}

# Define commands for building
# BUILD_ID can be generated for golang build no matter of debuginfo
%gobuild(o:) \
GOPPC64=power9 GOAMD64=v2 CGO_CPPFLAGS="-D_FORTIFY_SOURCE=2 -fstack-protector-all" go build -compiler gc -buildmode pie '-tags=rpm_crashtraceback libtrust_openssl ' -ldflags "-linkmode=external -compressdwarf=false ${LDFLAGS:-} -B 0x$(head -c20 /dev/urandom|od -An -tx1|tr -d ' \\n') -extldflags '%__global_ldflags'" -a -v -x %{?**};\

# Define commands for testing
%gotest() go test -compiler gc -ldflags "${LDFLAGS:-}" %{?**};
