Actually, you can do it without needing Lua, and you can use exactly the if [[ ... ]]
construct in BASH that you mentioned wanting to use in your original question.
To illustrate how, let's look at the header section of the specfile for the tmux
RPM I use. It must build unmodified on any host running RHEL/CentOS versions 6, 7, and 8 (and/or any other rebuilds, like Oracle's). This is what I have:
%global name tmux
%global version 3.1b
%global release 1%{?dist}
%global _hardened_build 1
Summary: A terminal multiplexer
Name: %{name}
Version: %{version}
Release: %{release}
# Mostly ISC-licensed, but some files in compat/ are 2-/3-clause BSD.
License: ISC/BSD
URL: https://tmux.github.io/
Source0: https://github.com/tmux/%{name}/releases/download/%{version}/%{name}-%{version}.tar.gz
Source1: bash_completion_tmux.sh
BuildRequires: %(/bin/bash -fc 'if [[ %{name} == tmux* ]]; then echo make ; else echo nomake ; fi') %(/bin/bash -fc 'if [[ %{name} == wobble* ]]; then echo wobble ; fi')
BuildRequires: gcc, ncurses-devel, %{expand:%(/bin/bash -c 'if [[ %{?rhel}%{!?rhel:9} -le 6 ]]; then echo libevent2-devel ; else echo libevent-devel ; fi')}
Requires: %{expand:%(/bin/bash -c 'if [[ %{?rhel}%{!?rhel:9} -le 6 ]]; then echo libevent2 ; else echo libevent ; fi')} >= 2.0
The tricky part with this RPM is that RHEL6 supplies the libevent
package, but it's version 1.4.13, which is too old for tmux
to build against successfully. (It requires libevent
2.x.) The good news, though, is that RHEL6 actually has both versions! The libevent2
RPM has version 2.0.21, which is recent enough to work for tmux
. But I can't just list libevent2-devel
as my build dependency; that package is not available for RHEL 7 or 8 because their default libevent
RPM is version 2 already. Nor can I list libevent-devel
as the build dependency because, while the libevent
and libevent2
packages can be installed side-by-side, the libevent-devel
and libevent2-devel
RPMs conflict.
As you can see in my specfile above, my solution to this challenge is to use RPM macros to inject either the string libevent-devel
or libevent2-devel
into the BuildRequires:
header value based on the BASH-based comparison of the value of the %{rhel}
macro (to which /etc/rpm/macros.dist
assigns the value of 6
, 7
, or 8
on RHEL 6, 7, or 8, respectively). For RHEL6 (and prior, theoretically...RHEL5 is dead, and RHEL4 and earlier are super-duper-dead!), the latter value is used whereas the former value is used for RHEL7 and RHEL8.
I also added a couple contrived string-matching macro invocations just to illustrate that it works correctly. My actual specfile contains neither the first BuildRequires:
line nor the final Requires:
line (because it's unnecessary/redundant); I added them specifically for this exercise to demonstrate that the conditionals work for both build-time and run-time dependencies.
How do I know they worked? I checked:
$ rpm -qp --qf '[%|SOURCERPM?{%25{=NEVRA}}:{%21{=NEVR}.src}|.rpm: %{REQUIREFLAGS:deptype}: %{REQUIRENEVRS}\n]' tmux-3.1b-1.el6.x86_64.rpm tmux-3.1b-1.el6.src.rpm tmux-3.1b-1.el8.x86_64.rpm tmux-3.1b-1.el8.src.rpm | fgrep 'manual:'
tmux-3.1b-1.el6.x86_64.rpm: manual: libevent2 >= 2.0
tmux-3.1b-1.el6.src.rpm: manual: make
tmux-3.1b-1.el6.src.rpm: manual: gcc
tmux-3.1b-1.el6.src.rpm: manual: ncurses-devel
tmux-3.1b-1.el6.src.rpm: manual: libevent2-devel
tmux-3.1b-1.el8.x86_64.rpm: manual: libevent >= 2.0
tmux-3.1b-1.el8.src.rpm: manual: gcc
tmux-3.1b-1.el8.src.rpm: manual: libevent-devel
tmux-3.1b-1.el8.src.rpm: manual: make
tmux-3.1b-1.el8.src.rpm: manual: ncurses-devel
Note that the correct libevent2 >= 2.0
dependency (with the 2
on the end) shows up in the RHEL6 RPM; the RHEL8 RPM, on the other hand, shows libevent >= 2.0
(without the 2
on the end), again exactly as it should. Additionally, a build dependency on make
shows up in both SRPMs, and both nomake
and wobble
are absent, proving that the pattern match conditionals on tmux*
and wobble*
worked exactly as they should.