[Rpm-maint] [rpm-software-management/rpm] Macro Language - tips, best reference ? (Discussion #2094)
Panu Matilainen
notifications at github.com
Tue Jun 14 07:48:42 UTC 2022
Good question :+1:
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 or view it on GitHub:
https://github.com/rpm-software-management/rpm/discussions/2094#discussioncomment-2944689
You are receiving this because you are subscribed to this thread.
Message ID: <rpm-software-management/rpm/repo-discussions/2094/comments/2944689 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rpm.org/pipermail/rpm-maint/attachments/20220614/c4bf52b8/attachment-0001.html>
More information about the Rpm-maint
mailing list