[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Fedora-packaging] Correction to guidelines on %define vs %global



On 05/20/2009 03:23 AM, Panu Matilainen wrote:

Apologies for missing this when the recommendation of %global over
%define in Fedora guidelines was being discussed, but better late than
never...

https://fedoraproject.org/wiki/Packaging/Guidelines#.25global_preferred_over_.25define

says "The two macro defining statements behave the same when they are a
the top level of rpm's nesting level" but this is not correct:

The body of a %global macro is expanded at definition time, whereas the
body of a %define is expanded only when used. For many uses it doesn't
make any difference but identical they are not, even on top level - for
example see the attached example spec:

$ rpmbuild -bp macroex.spec 2>/dev/null|grep ^case
case1: first - second
case2: second - %{xxx}

Another example of this difference (in case of parametrized macros):
https://bugzilla.redhat.com/show_bug.cgi?id=495658



Hi Panu, How does this look? (Note, I haven't tested any of this code yet as I'm running out the door. I wrote it just from how I understand your message and bz#495658). Also note that I don't know what causes rpm to clear the %defines that have been defined. If you can fill that in that would be great. If it's too complex, we can figure out some way to rephrase around it.

"""
%global and %define are both used to define macros inside rpm spec files. We generally prefer using %global over %define.

%define created macros when they are used in nested macro expansions (like in %{!?foo: ... } constructs theoretically only lasts until the end brace (local scope), while %global definitions have global scope. However, rpm currently doesn't clear the scope for the macros unless [Accurate information needed here]. So %define'd macros often last throughout a spec file. However, when they don't, it's often non-obvious why the spec file is failing as the failure is caused by something changing in another part of the spec file.

%global has another major difference from %define that can cause problems when you first write the macro. %global is evaluated at the time that it is written whereas %define is evaluated separately everytime it occurs in a spec file. As an example, if you do the following in a spec file::

<pre>
%build

%global foo FOO
%global bar %foo
%define baz %foo
echo %{bar} - %{baz}

%global foo FOOBAR
echo %{bar} - %{baz}

You get this:

FOO - FOO
FOO - FOOBAR
</pre>

One place this becomes apparent is parameterized macros:
<pre>
%build
%global print_arg echo "Hello %1"
%print_arg World
</pre>
because the %1 is evaluated when print_arg is defined, this prints
<code>Hello</code> rather than "Hello World".

Escaping the % fixes this usage:

<pre>
%global print_arg echo "Hello %%1"
</pre>
"""

-Toshio


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]