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

JVDptt notifications at github.com
Sat Jun 18 17:35:07 UTC 2022


OK, I found in the macro reference page :
"Note that in rpm >= 4.17, conditionals on built-in macros simply test for
existence of that built-in, just like with any other macros. In older
versions, the behavior of conditionals on built-ins is undefined."
but I am not testing a built-in here ?
When I change 'is_enabled' to just be '"%{%1}"==1?"%2":"%3",
I do get :
warning: 1==1?OK:NOT_OK
so, it looks like 'expr' in v4.14.3 does not honor the ternary conditional
operator 'A?B:C' ?
Is it meant to ?  Where is the list of expressions that expr supports
listed for v4.14.3 ?



On Fri, 17 Jun 2022 at 18:33, Jason Vas Dias ***@***.***> wrote:

> Or, more simply:
>
> v4.17.0:
>
> $ rpm --eval '%define is_enabled() %{expr "%{%1}"=="1"?"%2":"%3"}
> %global a_var 1
> %{warn %{is_enabled a_var OK NOT_OK}}'
> warning: OK
>
> v4.14.3:
>
> $ rpm --eval '%define is_enabled() %{expr "%{%1}"=="1"?"%2":"%3"}
> > %global a_var 1
> > %{warn %{is_enabled a_var OK NOT_OK}}'
> warning:
> $
>
>
> On Fri, 17 Jun 2022 at 18:27, Jason Vas Dias ***@***.***>
> wrote:
>
>> Aargh !
>>
>> Why does this work with only RPM  v4.17 (Fedora 36), not with RPM v4.14.3
>> (Rocky EL8) :
>>
>> with RPM v4.17:
>> $ rpm --eval '%define is_enabled() %["%{expr %{%1}}"=="1"?"%2":"%3"]
>> %global a_var 1
>> %{warn:%{is_enabled a_var OK NOT_OK}}'
>> warning: OK
>> $
>>
>> with RPM v4.14.3:
>> $ rpm --eval '%define is_enabled() %["%{expr %{%1}}"=="1"?"%2":"%3"]
>> %global a_var 1
>> %{warn %{is_enabled a_var OK NOT_OK}}'
>> warning:
>> $
>>
>>
>> On Wed, 15 Jun 2022 at 18:44, Jason Vas Dias ***@***.***>
>> wrote:
>>
>>> 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:
>>>> <rpm-software-management/rpm/repo-discussions/2094/comments/2944689@
>>>> github.com>
>>>>
>>>


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

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


More information about the Rpm-maint mailing list