From 8d427b56af9bf642c7d097ee16959d6140adb633 Mon Sep 17 00:00:00 2001 From: Nicolas Mailhot Date: Dec 02 2018 16:01:58 +0000 Subject: clean up lua file hierarchy --- diff --git a/lua/rpm/go.lua b/lua/rpm/go.lua new file mode 100644 index 0000000..525443c --- /dev/null +++ b/lua/rpm/go.lua @@ -0,0 +1,64 @@ +-- Copyright (c) 2017-2018 Nicolas Mailhot +-- This file is distributed under the terms of GNU GPL license version 3, or +-- any later version. +-- +-- This file contains Lua code used in rpm macros to create rpm packages past +-- the srpm stage: anything starting with %prep. +-- +-- The sister file gosrpm.lua deals with srpm and buildroot setup and is more +-- restricted. + +-- The goenv macro main processing function +-- See the documentation in the macros.go-rpm file for argument description +local function goenv(suffix, goipath, verbose, usermetadata) + local fedora = require "fedora.common" + local suffix = suffix + local goipath = goipath + local ismain = (suffix == "") or (suffix == "0") + if (goipath ~= "") and (suffix == "") then + local bettersuffix = fedora.getbestsuffix("goipath",goipath) + if (bettersuffix ~= nil) then + suffix = bettersuffix + end + end + if (goipath == "") then + goipath = "%{goipath" .. suffix .. "}" + end + suffixes = fedora.getsuffixes("goipath") + fedora.safeset( "goworkdir", "%{_builddir}/%{extractdir" .. suffixes[#suffixes] .. "}", verbose) + fedora.safeset( "gobuilddir", "%{goworkdir}/_build", verbose) + if ismain then + fedora.zalias({"gosourcedir","gofilelist"}, verbose) + else + fedora.safeset("gofilelist" .. suffix, rpm.expand("%gorpmname %{goipath" .. suffix .. "}") .. "-%{gofilelist}", verbose) + end + fedora.safeset( "gosourcedir" .. suffix, "%{?extractdir" .. suffix .. ":%{_builddir}/%{extractdir" .. suffix .. "}}" .. + "%{!?extractdir" .. suffix .. ":%{goworkdir}}", verbose) + if ismain then + fedora.zalias({"gosourcedir"}, verbose) + end + local ldflags = "" + local metadata = { version = { id = "version" }, tag = {}, commit = {}, branch = {}, } + table.sort(metadata) + for m,_ in pairs(metadata) do + metadata[m]["id"] = metadata[m]["id"] or "version." .. m + metadata[m]["value"] = usermetadata[m] or rpm.expand("%{?" .. m .. suffix .. "}") + if (metadata[m]["value"] ~= '') then + ldflags = ldflags .. " -X " .. goipath .. "/" .. metadata[m]["id"] .. "=" .. metadata[m]["value"] + end + end + for _, v in ipairs({"goipath", "gosourcedir", "gofilelist", + "version", "tag", "commit", "branch"}) do + if (rpm.expand("%{?" .. v .. suffix .. "}") ~= "") then + fedora.explicitset( "current" .. v, "%{" .. v .. suffix .. "}", verbose) + else + fedora.explicitunset("current" .. v, verbose) + end + end + fedora.explicitset( "currentgoldflags", ldflags, verbose) +end + +return { + goenv = goenv, +} + diff --git a/lua/srpm/go.lua b/lua/srpm/go.lua new file mode 100644 index 0000000..8e931bb --- /dev/null +++ b/lua/srpm/go.lua @@ -0,0 +1,100 @@ +-- Copyright (c) 2017-2018 Nicolas Mailhot +-- This file is distributed under the terms of GNU GPL license version 3, or +-- any later version. +-- +-- This file contains Lua code used in rpm macros needed to create and process +-- source rpm (srpm) Go (golang) packages. +-- +-- The resulting code must not use any package except redhat-rpm-macros + +-- Sanitize a Go import path that can then serve as rpm package name +-- Mandatory parameter: a Go import path +local function gorpmname(goipath) + -- lowercase and end with '/' + goname = string.lower(goipath .. "/") + -- remove eventual protocol prefix + goname = string.gsub(goname, "^http(s?)://", "") + -- remove eventual .git suffix + goname = string.gsub(goname, "%.git/*", "") + -- remove eventual git. prefix + goname = string.gsub(goname, "^git%.", "") + -- add golang prefix + goname = "golang-" .. goname + -- remove FQDN root (.com, .org, etc) + goname = string.gsub(goname, "^([^/]+)%.([^%./]+)/", "%1/") + -- special-case x.y.z number-strings as that’s an exception in our naming + -- guidelines + repeat + goname, i = string.gsub(goname, "(%d)%.(%d)", "%1:%2") + until i == 0 + -- replace various separators rpm does not like with - + goname = string.gsub(goname, "[%._/%-]+", "-") + -- because of the Azure sdk + goname = string.gsub(goname, "%-for%-go%-", "-") + -- Tokenize along - separators and remove duplicates to avoid + -- golang-foo-foo-bar-foo names + local result = "" + local tokens = {} + tokens["go"] = true + for token in string.gmatch(goname, "[^%-]+") do + if not tokens[token] then + result = result .. "-" .. token + tokens[token] = true + end + end + -- reassemble the string, restore x.y.z runs, convert the vx.y.z + -- Go convention to x.y.z as prefered in rpm naming + result = string.gsub(result, "^-", "") + result = string.gsub(result, ":", ".") + -- some projects have a name that end up in a number, and *also* add release + -- numbers on top of it, keep a - prefix before version strings + result = string.gsub(result, "%-v([%.%d])", "-%1") + return(result) +end + +-- The gometa macro main processing function +-- See the documentation in the macros.go-srpm file for argument description +local function gometa(suffix, verbose, informative, silent) + local fedora = require "fedora.common" + local forge = require "fedora.srpm.forge" + local zsuffix = "" + if (suffix ~= "") then + zsuffix = " -z " .. suffix + end + local ismain = (suffix == "") or (suffix == "0") + if ismain then + fedora.zalias({"forgeurl", "goipath", "goname", "gourl", "gosource"}, verbose) + end + local spec = {} + for _, v in ipairs({'goipath', 'forgeurl'}) do + spec[v] = rpm.expand("%{?" .. v .. suffix .. "}") + end + -- All the Go packaging automation relies on goipath being set + if (spec["goipath"] == "") then + rpm.expand("%{error:Please set the Go import path in the %%{goipath" .. suffix .. "} variable before calling %%gometa" .. zsuffix .. "!}") + end + if (spec["forgeurl"] ~= "") then + fedora.safeset("gourl" .. suffix, "%{forgeurl" .. suffix .. "}",verbose) + else + fedora.safeset("gourl" .. suffix, "https://%{goipath" .. suffix .. "}",verbose) + fedora.safeset("forgeurl" .. suffix, "%{gourl" .. suffix .. "}",verbose) + end + forge.forgemeta(suffix, verbose, informative, silent) + if (rpm.expand("%{?forgesource" .. suffix .. "}") ~= "") then + fedora.safeset("gosource" .. suffix, "%{forgesource" .. suffix .. "}",verbose) + else + fedora.safeset("gosource" .. suffix, "%{gourl" .. suffix .. "}/%{archivename" .. suffix .. "}.%{archiveext" .. suffix .. "}",verbose) + end + fedora.safeset( "goname" .. suffix, "%gorpmname %{goipath" .. suffix .. "}",verbose) + fedora.zalias({"forgeurl","goname","gourl","gosource"},verbose) + -- Final spec variable summary if the macro was called with -i + if informative then + rpm.expand("%{echo:Packaging variables read or set by %%gometa}") + fedora.echovars({"goipath","goname","gourl","gosource"}, suffix) + end +end + +return { + gorpmname = gorpmname, + gometa = gometa, +} diff --git a/rpm/macros.d/macros.go-rpm b/rpm/macros.d/macros.go-rpm index 934997f..2f67b32 100644 --- a/rpm/macros.d/macros.go-rpm +++ b/rpm/macros.d/macros.go-rpm @@ -58,7 +58,7 @@ # -C default: %{commit} # -B default: %{branch} %goenv(z:i:vV:T:C:B:) %{lua: -local gorpm = require "rpmmacros.gorpm" +local go = require "fedora.rpm.go" local suffix = rpm.expand("%{?-z*}") local goipath = rpm.expand("%{?-i*}") local verbose = (rpm.expand("%{-v}") ~= "") @@ -68,7 +68,7 @@ for m,_ in pairs(margs) do local v = rpm.expand("%{?-" .. margs[m] .. "*}") if (v ~= "") then mvalues[m] = v end end -gorpm.goenv(suffix, goipath, verbose, mvalues) +go.goenv(suffix, goipath, verbose, mvalues) } # Create default Go directories. Arguments: @@ -128,12 +128,12 @@ golist --imported --package-path %{?-i*}%{!-i:%{currentgoipath}} --skip-self --t # -r resolve BuildRequires, but do not install them # -v be verbose %goprep(z:ai:b:s:kerv) %{lua: -local rpmmacros = require "rpmmacros.common" -local extract = (rpm.expand("%{-e}") == "") -local installdeps = (rpm.expand("%{-r}") == "") -local processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "") -local setupflags = rpm.expand("%{!-v:-q}") -local gomkdirflags = rpm.expand("%{?-i} %{?-b} %{?-s} %{-k} %{-v}") +local fedora = require "fedora.common" +local extract = (rpm.expand("%{-e}") == "") +local installdeps = (rpm.expand("%{-r}") == "") +local processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "") +local setupflags = rpm.expand("%{!-v:-q}") +local gomkdirflags = rpm.expand("%{?-i} %{?-b} %{?-s} %{-k} %{-v}") local buildrequires = {} local function process(suffix) local zsuffix = "" @@ -148,7 +148,7 @@ local function process(suffix) end -- Main loop if processall then - for _,s in pairs(rpmmacros.getsuffixes("goipath")) do + for _,s in pairs(fedora.getsuffixes("goipath")) do process(s) end else @@ -206,7 +206,7 @@ end # When invoked several times with different file list names, it will attribute # directories to the first file list that makes use of them. %goinstall(z:ai:b:s:o:O:ve:d:t:rV:T:C:B:p:g:) %{lua: -local rpmmacros = require "rpmmacros.common" +local fedora = require "fedora.common" local processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "") -- Main function local function process(suffix) @@ -230,7 +230,7 @@ local function process(suffix) end -- Main loop if processall then - for _,s in pairs(rpmmacros.getsuffixes("goipath")) do + for _,s in pairs(fedora.getsuffixes("goipath")) do process(s) end else @@ -262,7 +262,7 @@ end # recursive (subdirectories are excluded) # -r exclude files matching , %gocheck(z:ai:b:s:vd:t:rV:T:C:B:p:g:w) %{lua: -local rpmmacros = require "rpmmacros.common" +local fedora = require "fedora.common" local processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "") -- Main function local function process(suffix) @@ -287,25 +287,10 @@ local function process(suffix) end -- Main loop if processall then - for _,s in pairs(rpmmacros.getsuffixes("goipath")) do + for _,s in pairs(fedora.getsuffixes("goipath")) do process(s) end else process(rpm.expand("%{-z*}")) end } - -# Legacy compatibility macros. Do not use them. Should go away eventually -%gobuildroot %{expand: -%{warn:%%gobuildroot is obsolete, use %%goprep in %%prep instead!} -%goenv -z 0 -%gomkdir -k -ln -fs "%{gobuilddir}/bin" _bin -} - -%gochecksflags %{gocheckflags} - -%gochecks(z:ai:b:s:vd:t:rV:T:C:B:p:g:w) %{expand: -%{warn:%%gochecks is obsolete, use %%gocheck in %%check instead!} -%gocheck %{?**} -} diff --git a/rpm/macros.d/macros.go-rpm.deprecated b/rpm/macros.d/macros.go-rpm.deprecated new file mode 100644 index 0000000..1f22d52 --- /dev/null +++ b/rpm/macros.d/macros.go-rpm.deprecated @@ -0,0 +1,20 @@ +# RPM macros for Go packages. +# +# © 2017-2018 Nicolas Mailhot +# +# Legacy deprecated bits graveyard, for compatibility only +# Do not use, they *will* be removed + +%gobuildroot %{expand: +%{warn:%%gobuildroot is obsolete, use %%goprep in %%prep instead!} +%goenv -z 0 +%gomkdir -k +ln -fs "%{gobuilddir}/bin" _bin +} + +%gochecksflags %{gocheckflags} + +%gochecks(z:ai:b:s:vd:t:rV:T:C:B:p:g:w) %{expand: +%{warn:%%gochecks is obsolete, use %%gocheck in %%check instead!} +%gocheck %{?**} +} diff --git a/rpm/macros.d/macros.go-srpm b/rpm/macros.d/macros.go-srpm index 0abb15c..1bb90e9 100644 --- a/rpm/macros.d/macros.go-srpm +++ b/rpm/macros.d/macros.go-srpm @@ -25,8 +25,8 @@ # Sanitize a Go import path that can then serve as rpm package name # Mandatory parameter: a Go import path %gorpmname() %{lua: -local gosrpm = require "rpmmacros.gosrpm" -print(gosrpm.gorpmname(rpm.expand("%1"))) +local go = require "fedora.srpm.go" +print(go.gorpmname(rpm.expand("%1"))) } # Map Go information to rpm metadata. This macro will compute default spec @@ -89,17 +89,17 @@ print(gosrpm.gorpmname(rpm.expand("%1"))) %gometa(az:svi) %{lua: print( "BuildRequires: go-rpm-macros\\n") print(rpm.expand("ExclusiveArch: %{go_arches}\\n")) -local rpmmacros = require "rpmmacros.common" -local gosrpm = require "rpmmacros.gosrpm" +local fedora = require "fedora.common" +local go = require "fedora.srpm.go" local verbose = rpm.expand("%{-v}") ~= "" local informative = rpm.expand("%{-i}") ~= "" local silent = rpm.expand("%{-s}") ~= "" local processall = (rpm.expand("%{-a}") ~= "") and (rpm.expand("%{-z}") == "") if processall then - for _,s in pairs(rpmmacros.getsuffixes("goipath")) do - gosrpm.gometa(s,verbose,informative,silent) + for _,s in pairs(fedora.getsuffixes("goipath")) do + go.gometa(s,verbose,informative,silent) end else - gosrpm.gometa(rpm.expand("%{-z*}"),verbose,informative,silent) + go.gometa(rpm.expand("%{-z*}"),verbose,informative,silent) end }