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