Blame SOURCES/macros.go-srpm

2ecd80
# Copyright (c) 2015-2021 Jakub Cajka <jcajka@redhat.com>,
2ecd80
#                         Jan Chaloupka <jchaloup@redhat.com>,
2ecd80
#                         Nicolas Mailhot <nim@fedoraproject.org>
2ecd80
#                         Alejandro Saez Morollon <asm@redhat.com>
2ecd80
# This file is distributed under the terms of GNU GPL license version 3, or
2ecd80
# any later version.
2ecd80
2ecd80
# This file contains macros for building projects in golang for packages
2ecd80
# with golang compiler or gcc-go compiler based on an architecture.
2ecd80
# Golang is primarly for primary architectures, gcc-go for secondary.
2ecd80
#
2ecd80
# This file provides only macros and must not use any other package except
2ecd80
# redhat-rpm-macros.
2ecd80
2ecd80
# Define arches for PA and SA
2ecd80
%golang_arches   x86_64 %{arm} aarch64 ppc64le s390x
2ecd80
%gccgo_arches    %{mips}
2ecd80
%go_arches       %{golang_arches} %{gccgo_arches}
2ecd80
2ecd80
# Where to set GOPATH for builds
2ecd80
%gopath          %{_datadir}/gocode
2ecd80
2ecd80
# Define go_compilers macro to signal go-compiler package is available
2ecd80
%go_compiler     1
2ecd80
2ecd80
# Sanitize a Go import path that can then serve as rpm package name
2ecd80
# Mandatory parameter: a Go import path
2ecd80
%gorpmname() %{lua:
2ecd80
local goname = rpm.expand("%1")
2ecd80
-- lowercase and end with '/'
2ecd80
goname       = string.lower(goname .. "/")
2ecd80
-- remove eventual protocol prefix
2ecd80
goname       = string.gsub(goname, "^http(s?)://",         "")
2ecd80
-- add golang prefix
2ecd80
goname       = "golang-" .. goname
2ecd80
-- remove FQDN root (.com, .org, etc)
2ecd80
goname       = string.gsub(goname, "^([^/]+)%.([^%./]+)/", "%1/")
2ecd80
-- special-case x.y.z number-strings as that’s an exception in our naming
2ecd80
-- guidelines
2ecd80
repeat
2ecd80
  goname, i = string.gsub(goname, "(%d)%.(%d)",            "%1:%2")
2ecd80
until i == 0
2ecd80
-- replace various separators rpm does not like with -
2ecd80
goname       = string.gsub(goname, "[%._/%-]+",            "-")
2ecd80
-- because of the Azure sdk
2ecd80
goname       = string.gsub(goname, "%-for%-go%-",          "-")
2ecd80
-- Tokenize along - separators and remove duplicates to avoid
2ecd80
-- golang-foo-foo-bar-foo names
2ecd80
local result = ""
2ecd80
local tokens = {}
2ecd80
tokens["go"]     = true
2ecd80
tokens["git"]    = true
2ecd80
for token in string.gmatch(goname, "[^%-]+") do
2ecd80
   if not tokens[token] then
2ecd80
      result = result .. "-" .. token
2ecd80
      tokens[token] = true
2ecd80
   end
2ecd80
end
2ecd80
-- reassemble the string, restore x.y.z runs, convert the vx.y.z
2ecd80
-- Go convention to x.y.z as prefered in rpm naming
2ecd80
result = string.gsub(result, "^-", "")
2ecd80
result = string.gsub(result, ":", ".")
2ecd80
-- some projects have a name that end up in a number, and *also* add release
2ecd80
-- numbers on top of it, keep a - prefix before version strings
2ecd80
result = string.gsub(result, "%-v([%.%d])", "-%1")
2ecd80
print(result)
2ecd80
}
2ecd80
2ecd80
# Map Go information to rpm metadata. This macro will compute default spec
2ecd80
# variable values.
2ecd80
#
2ecd80
# The following spec variable MUST be set before calling the macro:
2ecd80
#
2ecd80
#   goipath   the packaged Go project import path
2ecd80
#
2ecd80
# The following spec variables SHOULD be set before calling the macro:
2ecd80
#
2ecd80
#   forgeurl  the project url on the forge, strongly recommended, if it can not
2ecd80
#             be deduced from goipath; alternatively, use -u <url>
2ecd80
#   Version   if applicable, set it with Version: <version>
2ecd80
#   tag       if applicable
2ecd80
#   commit    if applicable
2ecd80
#
2ecd80
# The macro will attempt to compute and set the following variables if they are
2ecd80
# not already set by the packager:
2ecd80
#
2ecd80
#   goname         an rpm-compatible package name derived from goipath
2ecd80
#   gosource       an URL that can be used as SourceX: value
2ecd80
#   gourl          an URL that can be used as URL: value
2ecd80
#
2ecd80
# It will delegate processing to the forgemeta macro for:
2ecd80
#
2ecd80
#   forgesource    an URL that can be used as SourceX: value
2ecd80
#   forgesetupargs the correct arguments to pass to %setup for this source
2ecd80
#                  used by %forgesetup and %forgeautosetup
2ecd80
#   archivename    the source archive filename, without extentions
2ecd80
#   archiveext     the source archive filename extensions, without leading dot
2ecd80
#   archiveurl     the url that can be used to download the source archive,
2ecd80
#                  without renaming
2ecd80
#   scm            the scm type, when packaging code snapshots: commits or tags
2ecd80
#
2ecd80
# If the macro is unable to parse your forgeurl value set at least archivename
2ecd80
# and archiveurl before calling it.
2ecd80
#
2ecd80
# Most of the computed variables are both overridable and optional. However,
2ecd80
# the macro WILL REDEFINE %{dist} when packaging a snapshot (commit or tag).
2ecd80
# The previous %{dist} value will be lost. Don’t call the macro if you don’t
2ecd80
# wish %{dist} to be changed.
2ecd80
#
2ecd80
# Optional parameters:
2ecd80
#   -u <url>  Ignore forgeurl even if it exists and use <url> instead. Note
2ecd80
#             that the macro will still end up setting <url> as the forgeurl
2ecd80
#             spec variable if it manages to parse it.
2ecd80
#   -s  Silently ignore problems in forgeurl, use it if it can be parsed,
2ecd80
#       ignore it otherwise.
2ecd80
#   -p  Restore problem handling, override -s.
2ecd80
#   -v  Be verbose and print every spec variable the macro sets.
2ecd80
#   -i  Print some info about the state of spec variables the macro may use or
2ecd80
#       set at the end of the processing.
2ecd80
%gometa(u:spvi) %{expand:%{lua:
2ecd80
local forgeurl    = rpm.expand("%{?-u*}")
2ecd80
if (forgeurl == "") then
2ecd80
  forgeurl        = rpm.expand("%{?forgeurl}")
2ecd80
end
2ecd80
-- Be explicit about the spec variables we’re setting
2ecd80
local function explicitset(rpmvariable,value)
2ecd80
  rpm.define(rpmvariable .. " " .. value)
2ecd80
  if (rpm.expand("%{?-v}") ~= "") then
2ecd80
    rpm.expand("%{echo:Setting %%{" .. rpmvariable .. "} = " .. value .. "\\n}")
2ecd80
  end
2ecd80
end
2ecd80
-- Never ever stomp on a spec variable the packager already set
2ecd80
local function safeset(rpmvariable,value)
2ecd80
  if (rpm.expand("%{?" .. rpmvariable .. "}") == "") then
2ecd80
    explicitset(rpmvariable,value)
2ecd80
  end
2ecd80
end
2ecd80
-- All the Go packaging automation relies on goipath being set
2ecd80
local goipath = rpm.expand("%{?goipath}")
2ecd80
if (goipath == "") then
2ecd80
  rpm.expand("%{error:Please set the Go import path in the “goipath” variable before calling “gometa”!}")
2ecd80
end
2ecd80
-- Compute and set spec variables
2ecd80
if (forgeurl ~= "") then
2ecd80
  rpm.expand("%forgemeta %{?-v} %{?-i} %{?-s} %{?-p} -u " .. forgeurl .. "\\n")
2ecd80
  safeset("gourl", forgeurl)
2ecd80
else
2ecd80
  safeset("gourl", "https://" .. goipath)
2ecd80
  rpm.expand("%forgemeta %{?-v} %{?-i} -s     %{?-p} -u %{gourl}\\n")
2ecd80
end
2ecd80
if (rpm.expand("%{?forgesource}") ~= "") then
2ecd80
  safeset("gosource", "%{forgesource}")
2ecd80
else
2ecd80
  safeset("gosource", "%{gourl}/%{archivename}.%{archiveext}")
2ecd80
end
2ecd80
safeset("goname", "%gorpmname %{goipath}")
2ecd80
-- Final spec variable summary if the macro was called with -i
2ecd80
if (rpm.expand("%{?-i}") ~= "") then
2ecd80
  rpm.expand("%{echo:Go-specific packaging variables}")
2ecd80
  rpm.expand("%{echo:  goipath:         %{?goipath}}")
2ecd80
  rpm.expand("%{echo:  goname:          %{?goname}}")
2ecd80
  rpm.expand("%{echo:  gourl:           %{?gourl}}")
2ecd80
  rpm.expand("%{echo:  gosource:        %{?gosource}}")
2ecd80
end}
2ecd80
BuildRequires: compiler(go-compiler)
2ecd80
ExclusiveArch: %{go_arches}
2ecd80
}
2ecd80
2ecd80
# Define commands for building
2ecd80
# BUILD_ID can be generated for golang build no matter of debuginfo
2ecd80
%gobuild(o:) \
2ecd80
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 %{?**};\
2ecd80
2ecd80
# Define commands for testing
2ecd80
%gotest() go test -compiler gc -ldflags "${LDFLAGS:-}" %{?**};