Questo contenuto non è disponibile nella lingua selezionata.
Chapter 3. RPM macros
An RPM macro is a straight text substitution that, in addition to simple literal substitution, can perform various string operations, evaluate expressions, interact with the OS, and other actions.
3.1. Displaying the built-in RPM macros Copia collegamentoCollegamento copiato negli appunti!
RPM provides multiple built-in macros that you can use to simplify package maintenance and make it consistent across packages.
Procedure
Display the built-in RPM macros:
rpm --showrc|grep "<builtin>”
$ rpm --showrc|grep "<builtin>”Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.2. Displaying RPM distribution macros Copia collegamentoCollegamento copiato negli appunti!
Different distributions provide distinct sets of recommended RPM macros based on the language implementation of the software being packaged or the specific guidelines of the distribution. The sets of recommended RPM macros are often provided as RPM packages, ready to be installed by using the DNF package manager. You can find the installed macro files in the /usr/lib/rpm/macros.d/ directory.
Procedure
Display the raw RPM macro definitions:
rpm --showrc
$ rpm --showrcCopy to Clipboard Copied! Toggle word wrap Toggle overflow Determine what a macro does and how it can be helpful when packaging RPMs:
rpm --showrc | grep <macro_name>
$ rpm --showrc | grep <macro_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Display information about the RPMs macros for your system version of RPM:
rpm -ql rpm | grep macros
$ rpm -ql rpm | grep macrosCopy to Clipboard Copied! Toggle word wrap Toggle overflow
3.3. Customizing RPM behavior by using macros Copia collegamentoCollegamento copiato negli appunti!
You can customize RPM behavior by editing the RPM macros. You can override any macros, except the built-in macros, in the ~/.rpmmacros file with your custom macros. Any changes that you make affect every build on every system sharing the same home directory.
To override macros on a per-machine basis, put these macros in a /etc/rpm/macros.* file.
It is not recommended to use new macros that you defined in the ~/.rpmmacros file in packaging. Such macros would not be present on other machines, where users might want to try to rebuild your package.
Procedure
Customize a macro:
%_topdir /opt/<directory>/rpmbuild
%_topdir /opt/<directory>/rpmbuildCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can create the
<directory>, including all subdirectories, by using therpmdev-setuptreeutility.Note that the value of this macro is by default
~/rpmbuild.
3.4. Defining custom RPM macros in a spec file Copia collegamentoCollegamento copiato negli appunti!
In addition to using the built-in and distribution RPM macros, you can define custom RPM macros to simplify package maintenance and make it consistent across packages. RPM spec files can use either the %define and %global macros.
The differences between the %define and %global macros are the following:
-
%definehas global scope, except when used in a parametric macro, where its scope is local to this macro. The body of the%definemacro is expanded when used. -
%globalhas global scope. The body of the%globalmacro is expanded at definition time.
Use %global macros for the following actions:
- To avoid multiple redundant evaluations
- To define global macros inside parametric macros
Otherwise, use %define macros.
The %define and %global macro use the %global <name> <body> or %define <name>[(opts)] <body> pattern:
-
All whitespace, the preceding and succeeding, that surrounds
<body>is removed during the macro expansion. -
The macro name might be composed of alphanumeric characters and the underscore (
_) character. Inclusion of the
(opts)field is optional:-
Simple macros do not contain the
(opts)field. In this case, only recursive macro expansion is performed. -
Parametric options are function-like macros that accept arguments and possible options. For more details, see the
/usr/share/doc/rpm/macros.mdfile.
-
Simple macros do not contain the
Macros are evaluated and expanded everywhere in the spec file, even on lines commented by using the hash (#) and in spec file sections, such as %changelog.
To escape macro expansion, you can use the %dnl macro which comments out everything up to the next new line.
You can also escape macro expansion by placing a second percent sign (%) in front of the macro, for example %%{name}.
For more information about macros, see the /usr/share/doc/rpm/macros.md file.
Procedure
Define the macro. For example, you can include the following line in the RPM
specfile.%define date 20241114 %define upstream_version 2.5.4-pre1
%define date 20241114 %define upstream_version 2.5.4-pre1Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.5. Common RPM directives in the %files section of spec Copia collegamentoCollegamento copiato negli appunti!
The following are the RPM directives that you can use in the %files section of a spec file.
| Macro | Definition |
|---|---|
|
|
The |
|
|
The Important
Documentation can be excluded during package installation, for example, to conserve disk space on images. License files might look like documentation files, but they must always be installed with the software. Therefore, you must use the
The |
|
|
The Example usage: %dir %{_libdir}/%{name}
|
|
|
The Example usage: %config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
|
3.6. The %autosetup and %setup macros Copia collegamentoCollegamento copiato negli appunti!
You can use the %autosetup macro to unpack the source archives and apply patches automatically without manually specifying each patch by using the %patch directive.
%autosetup applies patches in the order they appear in the spec file. Always consider keeping sources and patches sorted by their number in spec to avoid unexpected actions. Alternatively, avoid the numbering entirely by using %patchlist or non-numbered Patch entries.
You can also use the %setup macro to unpack the source archives for building RPM packages.
Use the %autosetup macro whenever possible, instead of the %setup macro.
3.6.1. The %autosetup macro options Copia collegamentoCollegamento copiato negli appunti!
You can use the following %autosetup macro options to control its behavior. %autosetup also accepts all %setup macro options.
You can combine the %autosetup macro options.
| Macro option | Description |
|---|---|
|
|
Use the |
|
|
Use the |
|
|
Use the |
|
|
Use the |
3.6.2. The %autopatch macro options Copia collegamentoCollegamento copiato negli appunti!
The %autopatch macro applies all patches in the order stated in a spec file. You can use the following %autopatch macro options to control its behavior.
You can combine the %autopatch macro options.
| Macro option | Description |
|---|---|
|
|
Use the |
|
|
Use the |
|
|
Use the |
|
|
Use the |
|
|
Use the |
3.6.3. The %setup macro options Copia collegamentoCollegamento copiato negli appunti!
You can use the following %setup macro options for controlling the unpacking of source archives.
You can combine the %setup macro options.
| Macro option | Description |
|---|---|
|
|
Use the |
|
|
Use the
For example, you can use this option when the directory from the expanded source code archive has a different name from what is expected ( Name: cello
Source0: https://example.com/%{name}/release/hello-%{version}.tar.gz
...
%prep
%setup -n hello
|
|
|
Use the /usr/bin/mkdir -p cello-1.0 cd 'cello-1.0'
The directory is not changed after archive expansion. |
|
|
Use the rm -rf 'cello-1.0'
|
|
|
Use the /usr/bin/gzip -dc '/builddir/build/SOURCES/cello-1.0.tar.gz' | /usr/bin/tar -xvvof -
|
|
|
Use the
For example, examples are provided in a separate Source0: https://example.com/%{name}/release/%{name}-%{version}.tar.gz
Source1: %{name}-%{version}-examples.tar.gz
...
%prep
%setup -b 1
|
|
|
Use the
For example, the Source0: https://example.com/%{name}/release/%{name}-%{version}.tar.gz
Source1: examples.tar.gz
...
%prep
%setup -a 1
|