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%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
- “If expression is true, then do some action”:
- 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.
Example usage of the %if conditionals Copy linkLink copied!
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 Copy linkLink copied!
%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 Copy linkLink copied!
%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 Copy linkLink copied!
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.
| Conditional | Description |
|---|---|
|
|
Use the %ifarch i386 sparc
...
%endif
All the contents of the |
|
|
The %ifnarch aarch64
...
%endif
All the contents of the |
|
|
Use the %ifos linux
...
%endif
All the contents of the spec file between |