Este conteúdo não está disponível no idioma selecionado.

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

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>”
    Copy to Clipboard Toggle word wrap

3.2. Displaying RPM distribution macros

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

  1. Display the raw RPM macro definitions:

    $ rpm --showrc
    Copy to Clipboard Toggle word wrap
  2. Determine what a macro does and how it can be helpful when packaging RPMs:

    $ rpm --showrc | grep <macro_name>
    Copy to Clipboard Toggle word wrap
  3. Optional: Display information about the RPMs macros for your system version of RPM:

    $ rpm -ql rpm | grep macros
    Copy to Clipboard Toggle word wrap

3.3. Customizing RPM behavior by using macros

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.

Note

To override macros on a per-machine basis, put these macros in a /etc/rpm/macros.* file.

Warning

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
    Copy to Clipboard Toggle word wrap

    You can create the <directory>, including all subdirectories, by using the rpmdev-setuptree utility.

    Note that the value of this macro is by default ~/rpmbuild.

3.4. Defining custom RPM macros in a spec file

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:

  • %define has global scope, except when used in a parametric macro, where its scope is local to this macro. The body of the %define macro is expanded when used.
  • %global has global scope. The body of the %global macro is expanded at definition time.
Important

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.md file.
Important

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 spec file.

    %define date 20241114
    %define upstream_version 2.5.4-pre1
    Copy to Clipboard Toggle word wrap

3.5. Common RPM directives in the %files section of spec

The following are the RPM directives that you can use in the %files section of a spec file.

Expand
Table 3.1. %files section directives
MacroDefinition

%license

The %license macro identifies the file listed as a license. RPM installs and labels this file as the license, for example, %license LICENSE.

%doc

The %doc macro identifies a file listed as a documentation file. RPM installs and labels this file as the documentation file, for example, %doc README.

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 %license macro instead of %doc for license files.

The %doc macro is used for documentation about the packaged software, code examples, and various accompanying items. Note that if code examples are included, you must be careful when removing executable mode from the file.

%dir

The %dir macro ensures that the path is a directory owned by the RPM package. The RPM file manifest then accurately detects which directories to clean up during the uninstall operation.

Example usage:

%dir %{_libdir}/%{name}
Copy to Clipboard Toggle word wrap

%config(noreplace)

The %config(noreplace) macro ensures that the file is a configuration file. This file, therefore, must not be overwritten or replaced during a package installation or update if the file has been modified from the original installation checksum. If there is a change, the file will be created with .rpmnew appended at the end of the filename upon upgrade or installation. Therefore, the pre-existing or modified file on the target system remains unchanged.

Example usage:

%config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
Copy to Clipboard Toggle word wrap

3.6. The %autosetup and %setup macros

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.

Important

%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.

Important

Use the %autosetup macro whenever possible, instead of the %setup macro.

3.6.1. The %autosetup macro options

You can use the following %autosetup macro options to control its behavior. %autosetup also accepts all %setup macro options.

Note

You can combine the %autosetup macro options.

Expand
Table 3.2. The %autosetup macro options
Macro optionDescription

-v

Use the -v option to enable the verbosity of the %autosetup macro.

-N

Use the -N option to disable automatic patch application.

-S<vcs_name>

Use the -S<vcs_name> option to specify the version control system (VCS) to use, for example, git_am, git, patch, or gendiff.

-p

Use the -p option to control patch prefix stripping. For more information, see the patch(1) man page on your system.

3.6.2. The %autopatch macro options

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.

Note

You can combine the %autopatch macro options.

Expand
Table 3.3. The %autopatch macro options
Macro optionDescription

-v

Use the -v option to enable the verbosity of the %autopatch macro.

-q

Use the -q option to turn off warnings if no matching patches are detected.

-p

Use the -p option to control patch prefix stripping. For more information, see the patch(1) man page on your system.

-m<patch_number>

Use the -m<patch_number> option to apply a range of patches starting from the specified patch number.

-M<patch_number>

Use the -M<patch_number> to apply a range of patches up to the specified patch number.

3.6.3. The %setup macro options

You can use the following %setup macro options for controlling the unpacking of source archives.

Note

You can combine the %setup macro options.

Expand
Table 3.4. The %setup macro options
Macro optionDescription

-q

Use the -q option to limit the verbosity of the %setup macro. Pass this option as the first option to the %setup macro.

-n

Use the -n option to specify the name of the directory from an expanded source code archive.

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>}-%{<version>}), which can lead to an error of the `%setup macro. For example, if the package name is cello, but the source code archive name is hello-1.0.tgz, and it contains the hello/ directory, the spec file must have the following contents:

Name: cello
Source0: https://example.com/%{name}/release/hello-%{version}.tar.gz
...
%prep
%setup -n hello
Copy to Clipboard Toggle word wrap

-c

Use the -c option to create a directory and then change to this directory before unpacking any sources, for example:

/usr/bin/mkdir -p cello-1.0
cd 'cello-1.0'
Copy to Clipboard Toggle word wrap

The directory is not changed after archive expansion.

-D

Use the -D option to disable deletion of the source code directory. You can use this option, for example, if the %setup macro is used several times. With the -D option, the following lines are not used:

rm -rf 'cello-1.0'
Copy to Clipboard Toggle word wrap

-T

Use the -T option to skip the default action of the %setup macro, which includes unpacking of the first source archive. This option removes the following line from the script:

/usr/bin/gzip -dc '/builddir/build/SOURCES/cello-1.0.tar.gz' | /usr/bin/tar -xvvof -
Copy to Clipboard Toggle word wrap

-b

Use the -b (before) option to expand specific sources before entering the working directory.

For example, examples are provided in a separate cello-1.0-examples.tar.gz archive, which expands into cello-1.0/examples. In this case, use -b 1 to expand Source1 before entering the working directory:

Source0: https://example.com/%{name}/release/%{name}-%{version}.tar.gz
Source1: %{name}-%{version}-examples.tar.gz
...
%prep
%setup -b 1
Copy to Clipboard Toggle word wrap

-a

Use the -a (after) to expand specific sources after entering the working directory. The argument is a source number from the spec file preamble section.

For example, the cello-1.0.tar.gz archive contains an empty examples directory. The examples are shipped in a separate examples.tar.gz archive, and they expand into the directory with the same name. In this case, use -a 1 to expand Source1 after entering the working directory:

Source0: https://example.com/%{name}/release/%{name}-%{version}.tar.gz
Source1: examples.tar.gz
...
%prep
%setup -a 1
Copy to Clipboard Toggle word wrap
Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat