Search code examples
rpmrpm-spec

Looking for a macro to expand a variable to the name + value


I'll use an example to show what I mean, since it's hard to explain.

I have a spec file that is used for native and cross-compiling. I have many variables being passed to rpmbuild using the --define option. I'd like a macro that would be the equivalent of this:

%{?myvar:myvar=%{myvar}}

So, iff myvar exists, I want it to expand to myvar=%{myvar} which then would be myvar=myval.

I tried playing with %{expand:} a little (src), but ran into trouble when I had more than one parameter. Since it seems a macro's parameters go to EOL, and I need them all on one line in the final output, that also becomes a requirement.

I don't know lua, which might help to iterate over the various parameters, e.g.:

%expvar myvar1 myvar2 myvar3 => myvar1=myval1 myvar2=myval2 myvar3=myval3

Thanks for any ideas!


Solution

  • I'm not sure I understand that bit about EOL but this works using lua.

    %expvar() %{lua: \
        for i = 1, rpm.expand("%#") do \
            local var = rpm.expand([[%]]..i) \
            local evar = rpm.expand([[%]]..var) \
            if [[%]]..var ~= evar then \
                print(var.."="..evar.." ") \
            end \
        end \
    }
    

    Use is:

    rpm -E 'blah %{expvar __cp __rm}foo'
    

    Actually, given my updated understanding of what you want I think this is a better answer (though this only operates on one macro at a time):

    %expvar() %{expand:%%{?%1:%1=%%{%1}}}
    

    Use is:

    rpm -E 'foo %{expvar __cp} %{expvar xx} %{expvar __rm} blah'
    

    In theory I think it should be possible to write that as a recursive macro so it can handle multiple arguments but I've never actually been able to make that sort of thing work.