|
On the advice of @pmatilai, Fedora is looking at updating its packaging guidelines to recommend One point of uncertainty that's come up, though, revolves around the expansion of things like subshell invocations. It's documented that Or will a shell invocation be executed immediately at time of definition, and its result substituted immediately? Re-execution at each expansion would be an expensive proposition for a macro that will be used multiple times in the spec file, for example: # A shortened git commit SHA
%define commit_sha 1eece1fe5544746f5cf91c8b81a2401a09fa457f
%define short_commit %(c=%{commit_sha} | echo ${c:0:7})
# Or a dependency version obtained from pkg-config
%define libssh_version %(pkg-config --modversion libssh 2>/dev/null || echo 0)The macros doc mentions that |
Replies: 2 comments 2 replies
|
A %define is 100% declarative, nothing is expanded at the time of definition. This is a hugely important aspect of it, eg to ensure macro files can be read without side-effects. Shell expansions are in no way special, so yes they're re-executed at the time of use. Sometimes this is what you want (eg inside parametric macros), sometimes it's not (eg the above examples). And yeah that's exactly what the "avoid re-expansion of expensive macros" refers to. The problem of %global is that it conflates two different issues: scope and time of expansion. For the expansive subshell stuff you'd really want one-shot time of use semantics instead - see #1155. I'll reply to the Fedora thread separately, but I think it's more useful to try to educate packagers about their difference and appropriate uses rather than make these sweeping everywhere-recommendations. That "everywhere" is exactly the problem with the current Fedora guidelines, because that's what leads to the anti-pattern of double-escaping stuff in %global to force lazy expansion etc. |
|
If there was example demonstrating where it is beneficial to use |
A %define is 100% declarative, nothing is expanded at the time of definition. This is a hugely important aspect of it, eg to ensure macro files can be read without side-effects.
Shell expansions are in no way special, so yes they're re-executed at the time of use. Sometimes this is what you want (eg inside parametric macros), sometimes it's not (eg the above examples). And yeah that's exactly what the "avoid re-expansion of expensive macros" refers to. The problem of %global is that it conflates two different issues: scope and time of expansion. For the expansive subshell stuff you'd really want one-shot time of use semantics instead - see #1155.
I'll reply to the Fedora thread separately…