nim / rpms / go-compilers

Forked from rpms/go-compilers 2 years ago
Clone

Blame macros.go-srpm

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