2

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!

Aaron D. Marasco
  • 6,506
  • 3
  • 26
  • 39

1 Answers1

1

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.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • The EOL was that there's no way to *end* the parameters to a macro (that I know of) except end-of-line. So you can do: `./configure %expvar(myvar1 myvar2 myvar3) SOMETHING=SomethingElse` – Aaron D. Marasco Jul 12 '15 at 21:41
  • Ah, you need the `%{macro}` format for that. Example usage added and code updated for spaces and not newlines. I only tested this with `rpm -E` so it might not work exactly in an rpm but I believe it should (or at least be very close). – Etan Reisner Jul 13 '15 at 02:10
  • It does mostly what I needed. However, it seems to run always, not only if the variable already exists which is what I really needed it for, e.g. `$ rpm -E 'blah %{expvar __cp xx}foo' blah __cp=/usr/bin/cp xx=%xx foo (expected) blah __cp=/usr/bin/cp foo ` Ugh... the formatting failed here... – Aaron D. Marasco Jul 13 '15 at 10:47
  • Ah, I missed that in the post. I can fix that but doesn't your example snippet do what you want then? `rpm -E 'foo%{?__cp:__cp=%{__cp}}%{?__xx:__xx=%{__xx}}bar'` -> `foo_cp=/bin/cpbar`? – Etan Reisner Jul 13 '15 at 12:55
  • Your second example is what I had before on my own, but didn't use the brace-enclosed syntax properly. The first one worked this morning, but somehow made the rest of the spec file invalid (it wasn't expanding {{_prefix}} any longer!?). Thanks! – Aaron D. Marasco Jul 13 '15 at 16:30