initCommon(); $template->displayHeader(); ?>

9.7. Defining Spec File Macros

The RPM system defines a lot of handy macros so that your spec files can work regardless of where system directories are located. You simply use the macro, such as %_bindir, in place of hard-coded paths. The %_bindir macro, for example, identifies the default directory for binary executables, /usr/bin.

Use these macros wherever possible to avoid hard-coded paths and settings.

9.7.1. Built-in macros

RPM includes a host of built-in macros, including the following useful directories:

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

The example directories shown above come from the standard RPM macro file, /usr/lib/rpm/macros, instead of the Red Hat-specific file, /usr/lib/rpm/redhat/macros, which holds:

%_prefix /usr

%_sysconfdir /etc

%_localstatedir /var

%_infodir /usr/share/info

%_mandir /usr/share/man

%_initrddir %{_sysconfdir}/rc.d/init.d

%_defaultdocdir %{_usr}/share/doc

9.7.2. Spec file-specific macros

Most of the pre-defined RPM macros hold directory paths or architecture information. RPM also includes a set of useful macros that you can use to help debug problematic spec files and well as perform common tasks in spec files. Table 10-5 lists these debugging and special spec file macros.

Table 10-5 Special spec-file macros

Macro

Usage

%dump

Prints out macro values

%{echo:message}

Prints message to stderr

%{error:message}

Prints message to stderr and returns BADSPEC

%{expand:expression}

Like eval, expands expression

%{F:file_exp}

Expands file_exp to a file name

%global name value

Defines a global macro

%{P:patch_exp}

Expands patch_exp to a patch file name

%{S:source_exp}

Expands source_exp to a source file name

%trace

Toggles the printing of debugging information

%{uncompress:filename}

Tests if file filename is compressed. If so, uncompresses and includes in the given context. If not compressed, calls cat to include file in given context.

%undefine macro

Undefines the given macro

%{warn:message}

Prints message to stderr

Note

To see the current list of macros, put a %dump at the start of your spec file.

9.7.3. Defining new macros

In addition to the built-in macros, you can define your own to make it easier to manage your packages. Define a new spec file macro with the following syntax:

%define macro_name value

For example:

%define major 2

%define minor 2

%define patchlevel 7

You can then use a macro with the %macro_name or %{macro_name} syntax. For example:

Version: %{major}.%{minor}.%{patchlevel}

You can also expand the results of running shell commands using a %(command) syntax with parenthesis instead of curly braces. For example:

%define today %(date)

9.7.4. Specifying parameters to macros

Most macros perform simple text substitution. You can also pass parameters to macros, and access those parameters within your macros, similarly to how shell scripts get command-line parameters.

Cross Reference

Chapter 15 covers shell scripting with RPM.

With parameters, you can expand the normal definition of a macro to the following:

%define macro_name(options) value

Any text within the parenthesis is passed to getopt(3), and acts as parameters to the macro. This is performed when the macro is expanded. You can also pass options to the macro using the %macro_name syntax (without curly braces). For example:

%foo 1 2 3

This example passes the parameters 1, 2, and 3 to the macro foo. Inside the macro, you can use a shell script-like syntax to access the parameters through special macros. Table 10-6 lists these macros.

Table 10-6 Parameter macros inside a macro expansion

Macro

Holds

%0

The name of the macro

%*

All the parameters to the macro, except for any processed options

%#

The number of parameters

%1

The first parameter

%2

The second parameter

%3

The third parameter, and so on with %4, %5 and beyond

%{-p}

Holds -p if the -p parameter was passed to the macro; otherwise holds nothing

%{-p*}

Holds the value passed with the -p parameter, if the -p parameter was passed to the macro; otherwise holds nothing

%{-p:text}

Holds text if the -p parameter was passed to the macro; otherwise holds nothing

Note that all parameters listed in Table 10-6 hold the remaining parameters after getopt(3) processing. You can use these macros within the definition of your own macros. You can also nest macros, such as the following:

%define mypatch() patch %{-p:-p%{-p*}}

This macro expands to the patch command if no -p parameter was passed. If you pass a -p parameter, such as -p 1, then the macro expands to -p with the value of the -p parameter:

patch -p1

Note

This type of syntax is used heavily with the patch command.

displayFooter('$Date: 2005/11/02 19:30:06 $'); ?>