Memoizing macro values
Florian Weimer
fweimer at redhat.com
Wed Dec 21 11:10:33 UTC 2022
* Miro Hrončok:
> 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.
Yes, that's similar to what I'd have done with Lua.
> 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
Could you bring this to Fedora (rawhide in 37 preferably)? I think it
will really help package maintainers. I did apply the workaround to
samba because it took a minute or so to process the spec file (they
follow the packaging guidelines to the letter and have hundreds of lines
with %python3_sitearch), but it would be nice to implement this
centrally.
Another package that benefits is python-pynn, and there I don't quite
see the source of the slowness.
Thanks,
Florian
More information about the Rpm-list
mailing list