6.2. spec file conditionals


By using spec file conditionals, you can enable conditional inclusion of various sections of the spec file.

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.

6.2.1. Example usage of the %if conditionals

The following are examples of the usage of %if RPM conditionals.

例 6.1. 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.

例 6.2. 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.

6.2.2. Specialized variants of %if conditionals

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.

Expand
表 6.4. Table
ConditionalDescription

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

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部