Blame SOURCES/macros.go-srpm

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