nim / rpms / go-compilers

Forked from rpms/go-compilers 6 years ago
Clone

Blame macros.go-srpm

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