Blob Blame History Raw
# EPEL5-specific macros

# This file is named such that it appears alphabetically after other macro
# files in /etc/rpm.  This allows us to overwrite RHEL-provided macros if
# necessary.  (It turns out that it is necessary; see _font_pkg below.)

# How this works:
# RPM for whatever reason allows the redifinition of the section specifiers:
# %description, %prep, %build, %install, and even %files.  This gives us a
# convenient place to hang new tags (just before %description is emitted) and
# to add things to the beginning of a section.

# Also, note that lua state is global unless declared local.  So non-local
# variables, as well as functions, defined in one macro are available to
# others.

#
# ================
# Some utility functions
#
# This turns debug output on or off.
# Wanted to make this a toggle, but rpm will re-parse the sec when it sees
# BuildArch:, so this just toggles again.
%epel_macros_trace() %{lua:
    local args = rpm.expand("%*")
    if args == "0" or args == "off" or args == "no" then
        trace = 0
    else
        trace = 1
    end
}

# Initializes global lua state and then undefines itself.
# Should be called before or as part of any macros below this one.
%epel_macros_init() %{lua:
    function db(str, nest)
        if type(trace) ~= "number" or trace == 0 then
            return
        end
        if type(nest) == "number" and nest > 0 then
            io.stderr:write(string.rep("| ", nest))
        end
        io.stderr:write(str .. "\\n")
    end
\
    db("Epel macros loading.")
\
    -- Return true if a particular macro is set to a "true" value, or false if not
    function istrue(macro)
        db("istrue <= " .. macro)
        local val = rpm.expand(macro)
        db(val)
        if val == "0" or val == "off" or val == "no" or val == macro then
            return False
        end
        if val == "1" or val == "on" or val == "yes" then
            return True
        end
        error("Error: macro " .. macro .. " has non-true/false value.")
    end
\
    -- Return true if a particular macro is set to a "true" value, or false if not
    function isfalse(macro)
        db("isfalse <= " .. macro)
        local val = rpm.expand('%' .. macro)
        if val == "" or val == "0" or val == "off" or val == "no" then
            return True
        end
        if val == "1" or val == "on" or val == "yes" then
            return False
        end
        error("Error: macro " .. macro .. " has non-true/false value.")
    end
\
    rpm.define("epel_macros_init %{nil}")
}

# ================

# This injects a BuildRoot: tag, a Group: tag and a %clean section by hooking
# %description. RPM requires that tags like BuildRoot:, Group: or License: be
# located before %description in the spec, so this is the perfect place to add
# them or change their values.  Of course, we don't provide either if the spec
# does.

# This also defines %license to the string "%doc" for use later in the %files
# section.

# What happens if the spec already has a %clean section?  We redefine %clean to
# "exit".  Assuming that %clean exists in he spec at some place before %files
# and after but not _immediately after %description, this means that what's in
# the spec's existing %clean section will just be appended to the previous
# section just after a call to exit.  So the old clean section will still be
# there but will have no effect.

%description %epel_macros_init%{lua:
    db("In %description.")
\
    local group = rpm.expand("%{?group}")
    if type(group) == "nil" or string.len(group) == 0 then
        -- What we use for Group: isn't important; it just has to be there
        db("No Group tag found; adding one.")
        print("Group: Unspecified\\n")
    else
        db("Group specified: " .. group)
    end
\
    local buildroot = rpm.expand("%{?buildroot}")
    if type(buildroot) == "nil" or string.len(buildroot) == 0 then
        -- Use the recommended form for BuildRoot
        db("No BuildRoot tag found; adding one.")
        buildroot = rpm.expand("%(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)")
        print("BuildRoot: " .. buildroot .. "\\n\\n")
    else
        db("Buildroot already specified: " .. buildroot)
    end
\
    if type(added_clean) ~= "number" or added_clean == 0 then
        db("Adding %clean.")
        print("%clean\\n")
        -- Note that we can't use %buildroot here, because rpm hasn\'t parsed the
        -- BuildRoot tag yet, and it doesn't parse macros in lua output anyway.
        print("rm -rf " .. buildroot .. "\\n\\n")
        added_clean = 1
    else
        db("Already added %clean.")
    end
    print("%description")
    --
    rpm.define("clean exit")
    --
    rpm.define("license %doc")
 }

# ================
# Add the usual buildroot cleaning to the very beginning of %install
%install %{lua:
    db("In %install.")
    print("%install\\n")
    print(rpm.expand("rm -rf %{buildroot}\\n"))
}