Add %new_package

%new_package is a wrapper around Name: and %package that abstracts their quirks
from packagers and macros. Its behavior is controled by the %{source_name}
global variable:
– when %{source_name} is not set, the first call to %new_package will create a
  Name: block and set %{source_name} to the %{name} of this block.
– when %{source_name} is set:
  – a call to %new_package with no arguments creates:
    Name: %{source_name}
  – otherwise, a call to %new_package creates the corresponding:
    %package…
    line, unless the resulting %{name} matches %{source_name}. In that case it
    creates:
    Name: %{source_name}
    as before.

Arguments:
– -n and %1 like %package
– -v to print the variables %new_package sets directly.

The intended use-case it to:
– simplify coordination between macros that create subpackages,
– make it easy for packagers to declare which of the macro-created packages
  owns the SRPM, and
– make %{source_name} available within spec files and not just as a dnf
  synthetic variable.

Unlike %{name} %{source_name} matches the SRPM name regardless of its location
within the spec file.
This commit is contained in:
Nicolas Mailhot 2020-04-21 19:39:15 +02:00
parent 2a236590f5
commit e416a7b3da
2 changed files with 81 additions and 5 deletions

View File

@ -9,7 +9,7 @@ local function explicitset(rpmvar, value, verbose)
end
rpm.define(rpmvar .. " " .. value)
if verbose then
rpm.expand("%{echo:Setting %%{" .. rpmvar .. "} = " .. value .. "}")
rpm.expand("%{warn:Setting %%{" .. rpmvar .. "} = " .. value .. "}")
end
end
@ -19,7 +19,7 @@ local function explicitunset(rpmvar, verbose)
if (rpm.expand("%{" .. rpmvar .. "}") ~= "%{" .. rpmvar .. "}") then
rpm.define(rpmvar .. " %{nil}")
if verbose then
rpm.expand("%{echo:Unsetting %%{" .. rpmvar .. "}}")
rpm.expand("%{warn:Unsetting %%{" .. rpmvar .. "}}")
end
end
end
@ -150,8 +150,9 @@ local function wordwrap(text)
for word in string.gmatch(line, "%s*[^%s]*\n?") do
local wl, bad = utf8.len(word)
if not wl then
print("%{warn: Invalid UTF-8 sequence detected in:\n" ..
word .. "\nIt may produce unexpected results.\n}")
print("%{warn:Invalid UTF-8 sequence detected in:}" ..
"%{warn:" .. word .. "}" ..
"%{warn:It may produce unexpected results.}")
wl = bad
end
if (pos == 0) then
@ -182,6 +183,45 @@ local function wordwrap(text)
return output
end
-- The processing core of %new_package
local function new_package(source_name, pkg_name, name_suffix, previous_name, verbose)
-- Safety net when the wrapper is used next to traditional syntax in a spec
if (source_name == "") and (previous_name ~= "") then
rpm.expand(
"%{warn:Something already set the following package name: " .. previous_name .. ".}" ..
"%{warn:However, %%{source_name} is empty. Please set the SRPM name in %%{source_name}!}")
if (name_suffix ~= "") then
print(rpm.expand("\n%package " .. name_suffix))
else
print(rpm.expand("\n%package -n " .. pkg_name))
end
return
end
-- New processing
if (pkg_name == "") and (name_suffix ~= "") then
pkg_name = name_suffix
if (source_name ~= "") then
pkg_name = source_name .. "-" .. name_suffix
end
end
if (pkg_name == "") then
if (source_name == "") then
rpm.expand("%{error:You need to set %%{source_name} or provide explicit package naming!}")
else
pkg_name = source_name
end
end
if (source_name == "") then
source_name = pkg_name
end
if (pkg_name == source_name) then
safeset("source_name", source_name, verbose)
print(rpm.expand("\nName: %{source_name}"))
else
print(rpm.expand("\n%package -n " .. pkg_name))
end
end
return {
explicitset = explicitset,
explicitunset = explicitunset,
@ -194,4 +234,5 @@ return {
getbestsuffix = getbestsuffix,
writevars = writevars,
wordwrap = wordwrap,
new_package = new_package,
}

View File

@ -1,4 +1,5 @@
# Some miscellaneous Fedora-related macros
# Some miscellaneous Fedora-related macros, intended to be used at rpmbuild -bs
# stage
# A directory for rpm macros
%rpmmacrodir /usr/lib/rpm/macros.d
@ -20,3 +21,37 @@ local fedora = require "fedora.common"
local variable = "%{" .. rpm.expand("%{-v*}%{!-v:_description}") .. "}"
print(fedora.wordwrap(variable))
}
# A wrapper around Name: and %package that abstracts their quirks from
# packagers and macros. Its behavior is controled by the %{source_name}
# global variable:
#  when %{source_name} is not set, the first call to %new_package will
# create a Name: block and set %{source_name} to the %{name} of this
# block.
#  when %{source_name} is set:
#  a call to %new_package with no arguments creates:
# Name: %{source_name}
# otherwise, a call to %new_package creates the corresponding:
# %package…
# line, unless the resulting %{name} matches %{source_name}. In that
# case it creates
# Name: %{source_name}
# as before.
# Arguments:
# -n and %1 like %package
# -v to print the variables %new_package sets directly.
# The intended use-case it to simplify coordination between macros that
# create subpackages, make it easy for packagers to declare which of the
# macro-created packages owns the SRPM, and make %{source_name} available
# within spec files and not just as a dnf synthetic variable.
# Unlike %{name} %{source_name} matches the SRPM name regardless of its
# location within the spec file.
%new_package(n:v) %{lua:
local fedora = require "fedora.common"
local source_name = rpm.expand("%{?source_name}")
local pkg_name = rpm.expand("%{-n*}")
local name_suffix = rpm.expand("%{?1}")
local previous_name = rpm.expand("%{?name}")
local verbose = (rpm.expand("%{-v}") ~= "")
fedora.new_package(source_name, pkg_name, name_suffix, previous_name, verbose)
}