[Rpm-maint] [rpm-software-management/rpm] Macro Language - tips, best reference ? (Discussion #2094)

JVDptt notifications at github.com
Wed Jun 15 17:45:21 UTC 2022


Best solution found :

$ rpm --eval '%define is_enabled() %["%{expr
%{%1}}"=="1"?"Enabled":"Disabled"]
%global a_var 1
%{warn:%{is_enabled:a_var}}'
warning: Enabled
$

Thanks Panu !

But I'm still not quite getting why this breaks it:
rpm --eval '%define is_enabled()
%["%{expr:%{%1}}"=="1"?"Enabled":"Disabled"]
  ie. just adding the colon after expr             ^- here
makes rpm emit : "error: parse error in expression: %{a_var}".
Why ? I am calling all other macros with ':' (colons) separating macro name
from parameters -
why can't I do that with expr ?

But anyway, problem solved ! Many thanks.

On Tue, 14 Jun 2022 at 08:48, Panu Matilainen ***@***.***>
wrote:

> Good question 👍
>
> A couple of fundamental problems there:
>
>    - function-like (aka parametric) macros, such as is_enabled() should
>    not be %global because with %global, the body is expanded at definition
>    time and any tests around eg %1 just melt away useless. Use %define
>    instead, and lot of weirdness goes away. Alternatively you can escape the
>    %1, but as a general guideline anything but simple constants, %define is
>    what you usually want. The "always use global" guidelines in various places
>    are misguided, really.
>    - the %{?%1} in is_enabled() tests whether that macro got an argument
>    or not, which is not what you want. Test with an expression instead.
>
> For example:
>
> [pmatilai🎩︎localhost ~]$ rpm --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>
> no
>
> [pmatilai🎩︎localhost ~]$ rpm --define "a_var 0" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>
> no
>
> [pmatilai🎩︎localhost ~]$ rpm --define "a_var 1" --eval '%["%{?a_var}"=="1"?"yes":"no"]'
>
> yes
>
>
> (Whether you use --eval or --define is a matter of style, but it's better
> to split stuff over multiple --define/--eval uses (they're simply processed
> in order), makes it easier to handle for us humans.)
>
> Turning that into a parametric macro gets you something like this:
>
> [pmatilai🎩︎localhost rpm]$ rpm --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>
> no
>
> [pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 0' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>
> no
>
> [pmatilai🎩︎localhost rpm]$ rpm --define 'a_var 1' --define '%is_enabled() %["%{?1}"=="1"?"yes":"no"]' --eval '%{is_enabled %a_var}'
>
> yes
>
>
> Note quoting around the %1 argument test: macros always expand to string
> values, so testing for integer value would only give you errors. The rest
> kinda depends what exactly you want to do with it.
>
> Hope that helps.
>
>> Reply to this email directly, view it on GitHub
> <https://github.com/rpm-software-management/rpm/discussions/2094#discussioncomment-2944689>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AZTWV4GZQJKVMWNKOXW6RS3VPA2NRANCNFSM5YVWQDZQ>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***
> .com>
>


-- 
Reply to this email directly or view it on GitHub:
https://github.com/rpm-software-management/rpm/discussions/2094#discussioncomment-2958046
You are receiving this because you are subscribed to this thread.

Message ID: <rpm-software-management/rpm/repo-discussions/2094/comments/2958046 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rpm.org/pipermail/rpm-maint/attachments/20220615/739c31f3/attachment.html>


More information about the Rpm-maint mailing list