spec file conditionals

Enable conditional inclusion of various sections of the spec file by using spec file conditionals.

Conditionals usually deal with the following aspects:

  • Architecture-specific sections.
  • Operating system-specific sections.
  • Compatibility issues between various versions of operating systems.

You can use spec conditionals for different purposes, for example:

  • Conditional expression (%if). You can use %if for multiple purposes. It can have, for example, the following syntax:
    • “If expression is true, then do some action”:
      %if expression
      ...
      %endif
    • “If expression is true, then do some action, otherwise, another action”:
      %if expression
      ...
      %elsif expression
      ...
      %else
      ...
      %endif
    • %if can also be followed by an arbitrary number of %elif conditionals (nested %elsif), for example:
      %if expression
      %elif expression
      ...
      %else
      %endif
  • System architecture (%ifarch, %ifnarch). %ifarch tests whether the current target system architecture matches. You can use %ifarch to build RPM packages for multiple platforms, for example:
    %ifarch s390 s390x
    BuildRequires: s390utils-devel
    %endif
  • Operating system (%ifos, %ifnos). %ifos controls spec file processing according to the build target operating system.

Example usage of the %if conditionals

Review the following examples that demonstrate how you can use %if conditionals within your spec file.

Using the %if conditional to handle compatibility between Red Hat Enterprise Linux 10 and other operating systems

%if 0%{?rhel} == 10
sed -i '/AS_FUNCTION_DESCRIBE/ s/^/#/' configure.in
sed -i '/AS_FUNCTION_DESCRIBE/ s/^/#/' acinclude.m4
%endif

When building a package on RHEL 10, this conditional comments out AS_FUNCTION_DESCRIBE lines from autoconf scripts being considered when the %rhel macro’s value is set to 10.

Using the %if conditional to handle definition of macros

%define ruby_archive %{name}-%{ruby_version}
%if 0%{?milestone:1}%{?revision:1} != 0
%define ruby_archive %{ruby_archive}-%{?milestone}%{?!milestone:%{?revision:r%{revision}}}
%endif

This conditional handles the definition of macros. If the %milestone or the %revision macros are set, the %ruby_archive macro, which defines the name of the upstream archive, is redefined.

Specialized variants of %if conditionals

Review the following specialized %if conditional statements that you can use within your spec file to specify system architectures or operating systems.

The specialized variants of the %if conditionals include the %ifarch, %ifnarch, and %ifos conditionals. These conditionals are commonly used and, therefore, have their own macros.

Table 1. Table
Conditional Description

%ifarch

Use the %ifarch conditional to begin a block of the spec file that is architecture-specific. The conditional is followed by one or more architecture specifiers, each separated by commas or whitespace, for example:

%ifarch i386 sparc ... %endif

All the contents of the spec file between %ifarch and %endif are processed only on the 32-bit AMD and Intel architectures or Sun SPARC-based systems.

%ifnarch

The %ifnarch conditional has a reverse logic than %ifarch conditional.

%ifnarch aarch64 ... %endif

All the contents of the spec file between %ifnarch and %endif are processed only if not done on a system with the 64-bit ARM architecture.

%ifos

Use the %ifos conditional to control processing based on the operating system of the build. The conditional can be followed by one or more operating system names, for example:

%ifos linux ...​ %endif

All the contents of the spec file between %ifos and %endif are processed only if the build was done on a Linux system.