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%iffor 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%ifcan also be followed by an arbitrary number of%elifconditionals (nested%elsif), for example:%if expression %elif expression ... %else %endif
System architecture (
%ifarch,%ifnarch).%ifarchtests whether the current target system architecture matches. You can use%ifarchto build RPM packages for multiple platforms, for example:%ifarch s390 s390x BuildRequires: s390utils-devel %endif-
Operating system (
%ifos,%ifnos).%ifoscontrolsspecfile 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.
| Conditional | Description |
|---|---|
|
|
Use the
All the contents of the |
|
|
The
All the contents of the |
|
|
Use the
All the contents of the spec file between |