Memoizing macro values
Miro Hrončok
mhroncok at redhat.com
Wed Dec 21 11:00:50 UTC 2022
On 21. 12. 22 11:40, Miro Hrončok wrote:
> On 21. 12. 22 11:17, Florian Weimer wrote:
>> In Fedora, we have this:
>>
>> /usr/lib/rpm/macros.d/macros.python:%python_sitelib %(RPM_BUILD_ROOT=
>> %{__python} -Esc "import sysconfig; print(sysconfig.get_path('purelib',
>> vars={'platbase': '%{_prefix}', 'base': '%{_prefix}'}))")
>> /usr/lib/rpm/macros.d/macros.python3:%python3_sitelib %(RPM_BUILD_ROOT=
>> %{__python3} -Ic "import sysconfig; print(sysconfig.get_path('purelib',
>> vars={'platbase': '%{_prefix}', 'base': '%{_prefix}'}))")
>>
>> This is a %define-style macro, so the macro gets re-evaluated for every
>> expansion. It can make spec file operations really, really slow.
>>
>> It's possible to work around this in spec files like this:
>>
>> %{?python3_sitearch: %global python3_sitearch %{python3_sitearch}}
>>
>> But this is quite ugly, and I wonder if there is a nice way to do it
>> directly in the macros file instead. I think I know how do it with Lua,
>> but maybe there is a better way?
>
> This works (using random to demonstarte the behavior:
>
> %_python3_random %(RPM_BUILD_ROOT= %{__python3} -Ic "import random;
> print(random.randint(0,100))")
> %python3_random %{?!_python3_random_cached:%{global _python3_random_cached
> %_python3_random}}%_python3_random_cached
>
> $ rpm --eval '%python3_random %python3_random %python3_random %python3_random
> %python3_random'
> 42 42 42 42 42
>
> Note that %python3_sitlib must be evaluated after Python is installed so maybe
> there must be some "non-empty" conditional added as well.
This should do:
%_python3_random %(RPM_BUILD_ROOT= %{__python3} -Ic "import random; print(i if
(i := random.randint(0,100)) > 75 else '')")
%python3_random %["%?_python3_random_cached" == "" ? "%{global
_python3_random_cached %_python3_random}" : ""]%_python3_random_cached
$ rpm --eval '%python3_random %python3_random %python3_random %python3_random
%python3_random'
87 87
--
Miro Hrončok
--
Phone: +420777974800
IRC: mhroncok
More information about the Rpm-list
mailing list