Starting with RPM 3.0, RPM macros have replaced most RPM settings from the rpmrc files.
An RPM macro defines a setting to the RPM system. A macro can be defined as a static value, such as the directory where the RPM database is installed. A macro can also be defined in terms of other macros. Furthermore, a macro can be defined with parameters.
In this case, the macro %_usr names the /usr directory. The macro %_usrsrc names the /usr/src directory, showing how to set a macro in terms of another macro.
RPM provides a number of places you can define macros, including inside spec files (probably the most common usage), in special macro initialization files, and on the command line.
In each case, the syntax is slightly different.
20.1.1.1. Defining Macros in Spec Files
You can define macros in most places in a spec file. With spec files, use the %define syntax to define a macro, which uses the following syntax:
%define name(options) body
The options are optional and can include the special values defined in Table 21-1.
Table 21-1 Special options for macros
These options are similar to those for shell scripting.
Cross Reference
In addition to these options, you can use a syntax of %{-a} to hold –a if an option of –a was passed. The syntax of %{-a*} indicates the value passed after the –a option.
You can assign a macro to the value returned by a shell command by using syntax like the following:
%(shell_command)
Note
This is similar to the $(shell_command) syntax supported by bash.
20.1.1.2. Defining Macros in Macro Initialization Files
Inside a macro file, define macros with the following syntax:
%macro_name value
Macros defined for the RPM system start with an underscore. Some older macros are left without the leading underscore.
Note
The macros defined with an underscore are not exported into rpm headers.
The %expand built-in macro will expand the value of something else, including executing a shell command. For example, the following sets the user’s home directory to the %home macro:
%home %{expand:%%(cd; pwd)}
Note that it is probably easier to simply set the %home macro in your per-user $HOME/.rpmmacros file to the name of your home directory rather than try to figure this out programmatically.
20.1.1.3. Defining Macros on the Command Line
The rpm command also lets you define macros with the --define option. The basic syntax is:
$ rpm --define 'macro_name value'
Note
Do not place the leading percent sign, %, on the macro you define with --define.
You can evaluate a macro or a macro expression with --eval. For example:
$ rpm --eval %_usrsrc
/usr/src
20.1.2. Customizing Macros
You can add your own macro definitions, using the syntax shown in the “Defining Macros in Macro Initialization Files” section. These macros are read on each invocation of the rpm or rpmbuild commands.
To add your custom macros, you must edit one of the macro definition files. Table 21-2 lists the macro definition files and their usage.
Table 21-2 RPM macro files
Note
Do not edit the /usr/lib/rpm/macros file, as this file gets overwritten when you upgrade to a new version of rpm.