Difference between revisions of "RPM Macros"

From SME Server
Jump to navigationJump to search
(Created page with "==Macro syntax== RPM has fully recursive spec file macros. Simple macros do straight text substitution. Parameterized macros include an options field, and perform argc/argv p...")
 
Line 1: Line 1:
==Macro syntax==
+
== Valid RPM Macros ==
  
RPM has fully recursive spec file macros. Simple macros do straight text substitution. Parameterized macros include an options field, and perform argc/argv processing on white space separated tokens to the next newline. During macro expansion, both flags and arguments are available as macros which are deleted at the end of macro expansion. Macros can be used (almost) anywhere in a spec file, and, in particular, in "included file lists" (i.e. those read in using %files -f \<file\>). In addition, macros can be nested, hiding the previous definition for the duration of the expansion of the macro which contains nested macros.
+
Here are the definitions for some common specfile macros as they are defined on Fedora 13 (rpm-4.8.0-14.fc13). For definitions of more macros, examine the output of "<code>rpm --showrc</code>". To see the expanded definition of a macro use the command <code>rpm --eval "%{macro}"</code>. Note that neither command will take into account macros defined inside specfiles, but both will take into account macros defined in your <code>~/.rpmmacros</code> file and macros defined on the command line.
  
  here the original document http://www.rpm.org/wiki/PackagerDocs/Macros
+
Keep in mind that some of these macros may evaluate differently on older Fedora or EPEL releases.  
  
===Defining a Macro===
+
=== Macros mimicking autoconf variables ===
 +
<pre>
 +
%{_sysconfdir}        /etc
 +
%{_prefix}            /usr
 +
%{_exec_prefix}      %{_prefix}
 +
%{_bindir}            %{_exec_prefix}/bin
 +
%{_libdir}            %{_exec_prefix}/%{_lib}
 +
%{_libexecdir}        %{_exec_prefix}/libexec
 +
%{_sbindir}          %{_exec_prefix}/sbin
 +
%{_sharedstatedir}    /var/lib
 +
%{_datarootdir}      %{_prefix}/share
 +
%{_datadir}          %{_datarootdir}
 +
%{_includedir}        %{_prefix}/include
 +
%{_infodir}          /usr/share/info
 +
%{_mandir}            /usr/share/man
 +
%{_localstatedir}    /var
 +
%{_initddir}          %{_sysconfdir}/rc.d/init.d
 +
</pre>
 +
{{Note box|msg=Differences in EPEL 4 & 5
 +
<code>%{_initddir}</code> does not exist in EPEL 4 & 5, use the deprecated <code>%{_initrddir}</code> macro instead
 +
<code>%{_sharedstatedir}</code> expands to <code>%{_prefix}/com</code> in EPEL 4 & 5
 +
}}
  
To define a macro use:
+
=== Other macros and variables for paths ===
 +
These macros should be used for paths that are not covered by the macros mimicking autoconf variables. The <code>%{buildroot}</code> macro or the <code>$RPM_BUILD_ROOT</code> variable is the directory that should be assumed to be the root file system when installing files. Is is used as the value for the <code>DESTDIR</code> variable.
 +
<pre>
 +
%{_var}              /var
 +
%{_tmppath}          %{_var}/tmp
 +
%{_usr}              /usr
 +
%{_usrsrc}            %{_usr}/src
 +
%{_lib}              lib (lib64 on 64bit multilib systems)
 +
%{_docdir}            %{_datadir}/doc
 +
%{buildroot}          %{_buildrootdir}/%{name}-%{version}-%{release}.%{_arch}
 +
$RPM_BUILD_ROOT      %{buildroot}
 +
</pre>
  
%define <name>[(opts)] <body>
+
=== Build flags macros and variables ===
 +
These macros should be used as flags for the compiler or linker. Note that the values for the macros below reflect the settings on Fedora 13 (i686) with redhat-rpm-config installed.
  
All whitespace surrounding \<body\> is removed. Name may be composed of alphanumeric characters, and the character `_' and must be at least 3 characters in length. A macro without an (opts) field is "simple" in that only recursive macro expansion is performed. A parameterized macro contains an (opts) field. The opts (i.e. string between parentheses) is passed exactly as is to getopt(3) for argc/argv processing at the beginning of a macro invocation. While a parameterized macro is being expanded, the following shell-like macros are available:
+
<pre>
 +
%{__global_cflags}  -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4
 +
%{optflags}          %{__global_cflags} -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables
 +
$RPM_OPT_FLAGS      %{optflags}
 +
</pre>
  
%0 the name of the macro being invoked
 
%* all arguments (unlike shell, not including any processed flags)
 
%** all arguments (including processed flags)
 
%# the number of arguments
 
%{-f} if present at invocation, the flag f itself
 
%{-f*} if present at invocation, the argument to flag f
 
%1, %2 the arguments themselves (after getopt(3) processing)
 
  
At the end of invocation of a parameterized macro, the above macros are (at the moment, silently) discarded.
+
=== RPM directory macros ===
===Writing a Macro===
+
The macros are usually used with <code>rpmbuild --define</code> to specify which directories rpmbuild should use, it is unusual to use them within SPEC files.
 +
<pre>
 +
%{_topdir}            %{getenv:HOME}/rpmbuild
 +
%{_builddir}          %{_topdir}/BUILD
 +
%{_rpmdir}            %{_topdir}/RPMS
 +
%{_sourcedir}        %{_topdir}/SOURCES
 +
%{_specdir}          %{_topdir}/SPECS
 +
%{_srcrpmdir}        %{_topdir}/SRPMS
 +
%{_buildrootdir}      %{_topdir}/BUILDROOT
 +
</pre>
 +
{{Note box|msg=Differences in EPEL 4 & 5
 +
<code>%{_buildrootdir}</code> does not exist in EPEL 4 & 5
 +
}}
  
Within the body of a macro, there are several constructs that permit testing for the presence of optional parameters. The simplest construct is "%{-f}" which expands (literally) to "-f" if -f was mentioned when the macro was invoked. There are also provisions for including text if flag was present using "%{-f:X}". This macro expands to (the expansion of) X if the flag was present. The negative form, "%{-f:Y}", expanding to (the expansion of) Y if -f was *not* present, is also supported.
+
[[Category:Packaging guidelines]]
 
 
In addition to the "%{...}" form, shell expansion can be performed using "%(shell command)". The expansion of "%(...)" is the output of (the expansion of) ... fed to /bin/sh. For example, "%(date +%%y%%m%%d)" expands to the string "YYMMDD" (final newline is deleted). Note the 2nd % needed to escape the arguments to /bin/date. There is currently an 8K limit on the size that this macro can expand to.
 
Builtin Macros
 
 
 
There are several builtin macros (with reserved names) that are needed to perform useful operations. The current list is
 
 
 
%trace toggle print of debugging information before/after expansion
 
%dump print the active (i.e. non-covered) macro table
 
%verbose is rpm in verbose mode?
 
 
%{echo:...} print ... to stderr
 
%{warn:...} print ... to stderr
 
%{error:...} print ... to stderr and return BADSPEC
 
 
 
%define ... define a macro
 
%undefine ... undefine a macro
 
%global ... define a macro whose body is available in global context
 
 
%{expand:...} like eval, expand ... to <body> and (re-)expand <body>
 
 
%{lua:...} expand with the embedded Lua interpreter
 
 
 
%{uncompress:...} expand ... to <file> and test to see if <file> is compressed.  The expansion is
 
cat <file> # if not compressed
 
gzip -dc <file> # if gzip'ed
 
bzip2 -dc <file> # if bzip'ed
 
%{basename:...} basename(1) macro analogue
 
%{dirname:...} dirname(1) macro analogue
 
%{suffix:...} expand to suffix part of a file name
 
%{url2path:...} convert url to a local path
 
%{getenv:...} getenv(3) macro analogue
 
%{getconfdir:...} expand to rpm "home" directory (typically /usr/lib/rpm)
 
 
%{S:...} expand ... to <source> file name
 
%{P:...} expand ... to <patch> file name
 
  %{F:...} expand ... to <file> file name
 
 
 
Note that %define and %global differ in more ways than just scope: the body of a %define'd macro is lazily expanded (ie when used), but the body of %global is expanded at definition time. It's possible to use %%-escaping to force lazy expansion of %global.
 
 
 
Macros may also be automatically included from /usr/lib/rpm/macros. In addition, rpm itself defines numerous macros. To display the current set, add "%dump" to the beginning of any spec file, process with rpm, and examine the output from stderr.
 
===Example of a Macro===
 
 
 
Here is an example %patch definition from /usr/lib/rpm/macros:
 
 
 
%patch(b:p:P:REz:) \
 
%define patch_file %{P:%{-P:%{-P*}}%{!-P:%%PATCH0}} \
 
%define patch_suffix %{!-z:%{-b:--suffix %{-b*}}}%{!-b:%{-z:--suffix %{-z*}}}%{!-z:%{!-b: }}%{-z:%{-b:%{error:Can't specify both -z(%{-z*}) and -b(%{-b*})}}} \
 
%{uncompress:%patch_file} | patch %{-p:-p%{-p*}} %patch_suffix %{-R} %{-E} \
 
...
 
 
 
The first line defines %patch with its options. The body of %patch is
 
 
 
%{uncompress:%patch_file} | patch %{-p:-p%{-p*}} %patch_suffix %{-R} %{-E}
 
 
 
The body contains 7 macros, which expand as follows
 
 
 
%{uncompress:...} copy uncompressed patch to stdout
 
%patch_file ... the name of the patch file
 
%{-p:...} if "-p N" was present, (re-)generate "-pN" flag
 
-p%{-p*} ... note patch-2.1 insists on contiguous "-pN"
 
%patch_suffix override (default) ".orig" suffix if desired
 
%{-R} supply -R (reversed) flag if desired
 
%{-E} supply -E (delete empty?) flag if desired
 
 
 
There are two "private" helper macros:
 
 
 
%patch_file the gory details of generating the patch file name
 
%patch_suffix the gory details of overriding the (default) ".orig"
 
 
 
===Using a Macro===
 
 
 
To use a macro, write:
 
 
 
%<name> ...
 
 
 
or
 
 
 
%{<name>}
 
 
 
The %{...} form allows you to place the expansion adjacent to other text. The %\<name\> form, if a parameterized macro, will do argc/argv processing of the rest of the line as described above. Normally you will likely want to invoke a parameterized macro by using the %\<name\> form so that parameters are expanded properly.
 
 
 
Example:
 
 
 
%define mymacro() (echo -n "My arg is %1" ; sleep %1 ; echo done.)
 
 
 
Usage:
 
 
 
%mymacro 5
 
 
 
This expands to:
 
 
 
(echo -n "My arg is 5" ; sleep 5 ; echo done.)
 
 
 
This will cause all occurrences of %1 in the macro definition to be replaced by the first argument to the macro, but only if the macro is invoked as "%mymacro 5". Invoking as "%{mymacro} 5" will not work as desired in this case.
 
===Command Line Options===
 
 
 
When the command line option "--define 'macroname value'" allows the user to specify the value that a macro should have during the build. Note lack of leading % for the macro name. We will try to support users who accidentally type the leading % but this should not be relied upon.
 
 
 
Evaluating a macro can be difficult outside of an rpm execution context. If you wish to see the expanded value of a macro, you may use the option
 
 
 
--eval '<macro expression>'
 
 
 
that will read rpm config files and print the macro expansion on stdout.
 
 
 
Note: This works only macros defined in rpm configuration files, not for macros defined in specfiles. You can use %{echo: %{your_macro_here}} if you wish to see the expansion of a macro defined in a spec file.
 
Configuration using Macros
 
 
 
Starting in rpm 3.0, macros rather than rpmrc lines are used to configure rpm. In general, all the rpmrc configuration lines documented in "Maximum RPM" have been converted to macros, usually with a leading underscore, and the same name that was used in rpmrc files. In some cases, there is no leading underscore. Those macros existed in rpm-2.5.x and the underscore is omitted in order to preserve the meaning and usage of macros that are defined during spec file parsing.
 
 
 
Here's an example to illustrate configuration using macros:
 
 
 
Old way:
 
In /etc/rpmrc and/or ~/.rpmrc you put
 
something:      some_value
 
 
 
New way:
 
In /etc/rpm/macros and/or ~/.rpmmacros
 
%_something    some_value
 
 
 
Here are 2 common FAQ for experienced users of rpm:
 
 
 
1) --rcfile works differently.
 
    Old way: rpm --rcfile whatever
 
    New way: rpm --rcfile /usr/lib/rpm/rpmrc:whatever
 
 
 
2) topdir (and other rpmrc configurables) work differently.
 
 
 
    Old way:
 
~/.rpmrc contains
 
topdir:        whatever
 
 
 
    New way:
 
/usr/lib/rpm/rpmrc contains
 
macrofiles:    /usr/lib/rpm/macros: ... :~/.rpmmacros
 
~/.rpmmacros contains
 
%_topdir        whatever
 
 
 
===Macro Analogues of Autoconf Variables===
 
 
 
Several macro definitions provided by the default rpm macro set have uses in packaging similar to the autoconf variables that are used in building packages:
 
 
 
    %_prefix /usr
 
    %_exec_prefix %{_prefix}
 
    %_bindir %{_exec_prefix}/bin
 
    %_sbindir %{_exec_prefix}/sbin
 
    %_libexecdir %{_exec_prefix}/libexec
 
    %_datadir %{_prefix}/share
 
    %_sysconfdir %{_prefix}/etc
 
    %_sharedstatedir %{_prefix}/com
 
    %_localstatedir %{_prefix}/var
 
    %_libdir %{_exec_prefix}/lib
 
    %_includedir %{_prefix}/include
 
    %_oldincludedir /usr/include
 
    %_infodir %{_prefix}/info
 
    %_mandir %{_prefix}/man
 

Revision as of 16:37, 16 December 2014

Valid RPM Macros

Here are the definitions for some common specfile macros as they are defined on Fedora 13 (rpm-4.8.0-14.fc13). For definitions of more macros, examine the output of "rpm --showrc". To see the expanded definition of a macro use the command rpm --eval "%{macro}". Note that neither command will take into account macros defined inside specfiles, but both will take into account macros defined in your ~/.rpmmacros file and macros defined on the command line.

Keep in mind that some of these macros may evaluate differently on older Fedora or EPEL releases.

Macros mimicking autoconf variables

 %{_sysconfdir}        /etc
 %{_prefix}            /usr
 %{_exec_prefix}       %{_prefix}
 %{_bindir}            %{_exec_prefix}/bin
 %{_libdir}            %{_exec_prefix}/%{_lib}
 %{_libexecdir}        %{_exec_prefix}/libexec
 %{_sbindir}           %{_exec_prefix}/sbin
 %{_sharedstatedir}    /var/lib
 %{_datarootdir}       %{_prefix}/share
 %{_datadir}           %{_datarootdir}
 %{_includedir}        %{_prefix}/include
 %{_infodir}           /usr/share/info
 %{_mandir}            /usr/share/man
 %{_localstatedir}     /var
 %{_initddir}          %{_sysconfdir}/rc.d/init.d
Important.png Note:
Differences in EPEL 4 & 5
%{_initddir} does not exist in EPEL 4 & 5, use the deprecated %{_initrddir} macro instead
%{_sharedstatedir} expands to %{_prefix}/com in EPEL 4 & 5


Other macros and variables for paths

These macros should be used for paths that are not covered by the macros mimicking autoconf variables. The %{buildroot} macro or the $RPM_BUILD_ROOT variable is the directory that should be assumed to be the root file system when installing files. Is is used as the value for the DESTDIR variable.

 %{_var}               /var
 %{_tmppath}           %{_var}/tmp
 %{_usr}               /usr
 %{_usrsrc}            %{_usr}/src
 %{_lib}               lib (lib64 on 64bit multilib systems)
 %{_docdir}            %{_datadir}/doc
 %{buildroot}          %{_buildrootdir}/%{name}-%{version}-%{release}.%{_arch}
 $RPM_BUILD_ROOT       %{buildroot}
 

Build flags macros and variables

These macros should be used as flags for the compiler or linker. Note that the values for the macros below reflect the settings on Fedora 13 (i686) with redhat-rpm-config installed.

 %{__global_cflags}   -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4
 %{optflags}          %{__global_cflags} -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables
 $RPM_OPT_FLAGS       %{optflags}


RPM directory macros

The macros are usually used with rpmbuild --define to specify which directories rpmbuild should use, it is unusual to use them within SPEC files.

 %{_topdir}            %{getenv:HOME}/rpmbuild
 %{_builddir}          %{_topdir}/BUILD
 %{_rpmdir}            %{_topdir}/RPMS
 %{_sourcedir}         %{_topdir}/SOURCES
 %{_specdir}           %{_topdir}/SPECS
 %{_srcrpmdir}         %{_topdir}/SRPMS
 %{_buildrootdir}      %{_topdir}/BUILDROOT
Important.png Note:
Differences in EPEL 4 & 5
%{_buildrootdir} does not exist in EPEL 4 & 5