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

22.2. Macros

You can define macros in your spec files to help control how the package gets built. The following section describes these macros.

22.2.1. Variable definition macros

The %define macro allows you to define new macros from within your spec file. A common usage is to define top-level directories with %define macros at the top of a spec file and then reference these directories throughout the file. For example:

%define_bindir/bin

This allows you to change the setting in one place, which is very handy for directory paths used throughout your spec files.

Cross Reference

See the section on Defining Macros in Spec Files in Chapter 21 for more on this subject.

You can use this syntax for other things that may commonly change, such as version numbers. For example:

%define major 2

%define minor 2

%define patchlevel 7

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

Table B-1 lists more special macros used within spec files.

Table B-1 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

22.2.2. Conditional macros

You can use a special syntax to test for the existence of macros. For example:

%{?macro_to_test: expression}

This syntax tells RPM to expand the expression if macro_to_test exists, otherwise ignore. A leading exclamation point, !, tests for the non-existence of a macro:

%{!?macro_to_test: expression}

In this example, if the macro_to_test macro does not exist, then expand the expression.

The %if macro performs an if test much like scripting languages. For example:

%if %{old_5x}

%define b5x 1

%undefine b6x

%endif

A %else allows you to specify what to do if the test is not successful. For example:

%if %{old_5x}

%define b5x 1

%undefine b6x

%else

%define b6x 1

%undefine b5x

%endif

Again, use an exclamation point to negate the test. For example:

%if ! %{old_5x}

%define b5x 1

%undefine b6x

%endif

You can use a && for an and test. For example:

%if %{old_5x} && %{old_6x}

%{error: You cannot build for .5x and .6x at the same time}

%quit

%endif

22.2.3. Built-in macros

The following macros are built into RPM and can help allow you to place your files in the right locations:

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

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