Florian Festi d0e93a
From 7b1fc619a5c828828dad7c1f61f525d957b9e2c5 Mon Sep 17 00:00:00 2001
Florian Festi d0e93a
From: Petr Viktorin <pviktori@redhat.com>
Florian Festi d0e93a
Date: Wed, 27 Jan 2021 17:32:51 +0100
Florian Festi d0e93a
Subject: [PATCH] Add %bcond macro for defining build conditionals
Florian Festi d0e93a
Florian Festi d0e93a
Move documentation from comments to reference manual
Florian Festi d0e93a
Fixes: https://github.com/rpm-software-management/rpm/issues/941
Florian Festi d0e93a
Florian Festi d0e93a
(cherry picked from commit a99b6373af0774f4bef62aa89defc84cfcacc078)
Florian Festi d0e93a
---
Florian Festi d0e93a
 macros.in                        | 54 +++++++----------------
Florian Festi d0e93a
 tests/Makefile.am                |  1 +
Florian Festi d0e93a
 tests/data/SPECS/bcondtest.spec  | 33 +++++++++++++++
Florian Festi d0e93a
 tests/rpmbuild.at                | 73 ++++++++++++++++++++++++++++++++
Florian Festi d0e93a
 5 files changed, 157 insertions(+), 42 deletions(-)
Florian Festi d0e93a
 create mode 100644 tests/data/SPECS/bcondtest.spec
Florian Festi d0e93a
Florian Festi d0e93a
diff --git a/macros.in b/macros.in
Florian Festi d0e93a
index 7c458f5d8a..35462c933c 100644
Florian Festi d0e93a
--- a/macros.in
Florian Festi d0e93a
+++ b/macros.in
Florian Festi d0e93a
@@ -78,47 +78,25 @@
Florian Festi d0e93a
 %defined()	%{expand:%%{?%{1}:1}%%{!?%{1}:0}}
Florian Festi d0e93a
 %undefined()	%{expand:%%{?%{1}:0}%%{!?%{1}:1}}
Florian Festi d0e93a
 
Florian Festi d0e93a
-# Shorthand for %{defined with_...}
Florian Festi d0e93a
+# Handle conditional builds.
Florian Festi d0e93a
+# (see 'conditionalbuilds' in the manual)
Florian Festi d0e93a
+#
Florian Festi d0e93a
+# Internally, the `--with foo` option defines the macro `_with_foo` and the
Florian Festi d0e93a
+# `--without foo` option defines the macro `_without_foo`.
Florian Festi d0e93a
+# Based on those and a default (used when neither is given), bcond macros
Florian Festi d0e93a
+# define the macro `with_foo`, which should later be checked:
Florian Festi d0e93a
+
Florian Festi d0e93a
+%bcond()	%[ (%2)\
Florian Festi d0e93a
+    ? "%{expand:%%{!?_without_%{1}:%%global with_%{1} 1}}"\
Florian Festi d0e93a
+    : "%{expand:%%{?_with_%{1}:%%global with_%{1} 1}}"\
Florian Festi d0e93a
+]
Florian Festi d0e93a
+%bcond_with()		%bcond %{1} 0
Florian Festi d0e93a
+%bcond_without()	%bcond %{1} 1
Florian Festi d0e93a
+
Florian Festi d0e93a
+# Shorthands for %{defined with_...}:
Florian Festi d0e93a
 %with()		%{expand:%%{?with_%{1}:1}%%{!?with_%{1}:0}}
Florian Festi d0e93a
 %without()	%{expand:%%{?with_%{1}:0}%%{!?with_%{1}:1}}
Florian Festi d0e93a
 
Florian Festi d0e93a
-# Handle conditional builds. %bcond_with is for case when feature is
Florian Festi d0e93a
-# default off and needs to be activated with --with ... command line
Florian Festi d0e93a
-# switch. %bcond_without is for the dual case.
Florian Festi d0e93a
-#
Florian Festi d0e93a
-# %bcond_with foo defines symbol with_foo if --with foo was specified on
Florian Festi d0e93a
-# command line.
Florian Festi d0e93a
-# %bcond_without foo defines symbol with_foo if --without foo was *not*
Florian Festi d0e93a
-# specified on command line.
Florian Festi d0e93a
-#
Florian Festi d0e93a
-# For example (spec file):
Florian Festi d0e93a
-#
Florian Festi d0e93a
-# (at the beginning)
Florian Festi d0e93a
-# %bcond_with extra_fonts
Florian Festi d0e93a
-# %bcond_without static
Florian Festi d0e93a
-# (and later)
Florian Festi d0e93a
-# %if %{with extra_fonts}
Florian Festi d0e93a
-# ...
Florian Festi d0e93a
-# %else
Florian Festi d0e93a
-# ...
Florian Festi d0e93a
-# %endif
Florian Festi d0e93a
-# %if ! %{with static}
Florian Festi d0e93a
-# ...
Florian Festi d0e93a
-# %endif
Florian Festi d0e93a
-# %if %{with static}
Florian Festi d0e93a
-# ...
Florian Festi d0e93a
-# %endif
Florian Festi d0e93a
-# %{?with_static: ... }
Florian Festi d0e93a
-# %{!?with_static: ... }
Florian Festi d0e93a
-# %{?with_extra_fonts: ... }
Florian Festi d0e93a
-# %{!?with_extra_fonts: ... }
Florian Festi d0e93a
-
Florian Festi d0e93a
-#
Florian Festi d0e93a
-# The bottom line: never use without_foo, _with_foo nor _without_foo, only
Florian Festi d0e93a
-# with_foo. This way changing default set of bconds for given spec is just
Florian Festi d0e93a
-# a matter of changing single line in it and syntax is more readable.
Florian Festi d0e93a
-%bcond_with()		%{expand:%%{?_with_%{1}:%%global with_%{1} 1}}
Florian Festi d0e93a
-%bcond_without()	%{expand:%%{!?_without_%{1}:%%global with_%{1} 1}}
Florian Festi d0e93a
 #
Florian Festi d0e93a
 #==============================================================================
Florian Festi d0e93a
 # ---- Required rpmrc macros.
Florian Festi d0e93a
diff --git a/tests/Makefile.am b/tests/Makefile.am
Florian Festi d0e93a
index 66cee3273b..6d41ef93c5 100644
Florian Festi d0e93a
--- a/tests/Makefile.am
Florian Festi d0e93a
+++ b/tests/Makefile.am
Florian Festi d0e93a
@@ -40,6 +40,7 @@ EXTRA_DIST += $(TESTSUITE_AT)
Florian Festi d0e93a
 
Florian Festi d0e93a
 ## testsuite data
Florian Festi d0e93a
 EXTRA_DIST += data/SPECS/attrtest.spec
Florian Festi d0e93a
+EXTRA_DIST += data/SPECS/bcondtest.spec
Florian Festi d0e93a
 EXTRA_DIST += data/SPECS/buildrequires.spec
Florian Festi d0e93a
 EXTRA_DIST += data/SPECS/docmiss.spec
Florian Festi d0e93a
 EXTRA_DIST += data/SPECS/hello.spec
Florian Festi d0e93a
diff --git a/tests/data/SPECS/bcondtest.spec b/tests/data/SPECS/bcondtest.spec
Florian Festi d0e93a
new file mode 100644
Florian Festi d0e93a
index 0000000000..7172a31d29
Florian Festi d0e93a
--- /dev/null
Florian Festi d0e93a
+++ b/tests/data/SPECS/bcondtest.spec
Florian Festi d0e93a
@@ -0,0 +1,33 @@
Florian Festi d0e93a
+Name:           bcondtest
Florian Festi d0e93a
+Version:        1.0
Florian Festi d0e93a
+Release:        1
Florian Festi d0e93a
+Group:          Testing
Florian Festi d0e93a
+License:        CC0
Florian Festi d0e93a
+BuildArch:      noarch
Florian Festi d0e93a
+Summary:        Test package for the bcond macro
Florian Festi d0e93a
+
Florian Festi d0e93a
+%bcond normally_on 1
Florian Festi d0e93a
+%bcond normally_off 0
Florian Festi d0e93a
+%bcond both_features %[%{with normally_on} && %{with normally_off}]
Florian Festi d0e93a
+
Florian Festi d0e93a
+%if %{with normally_on}
Florian Festi d0e93a
+Provides:       has_bcond(normally_on)
Florian Festi d0e93a
+%endif
Florian Festi d0e93a
+%if %{with normally_off}
Florian Festi d0e93a
+Provides:       has_bcond(normally_off)
Florian Festi d0e93a
+%endif
Florian Festi d0e93a
+%if %{with both_features}
Florian Festi d0e93a
+Provides:       has_bcond(both_features)
Florian Festi d0e93a
+%endif
Florian Festi d0e93a
+
Florian Festi d0e93a
+%description
Florian Festi d0e93a
+%{summary}
Florian Festi d0e93a
+
Florian Festi d0e93a
+%install
Florian Festi d0e93a
+mkdir -p %{buildroot}/opt
Florian Festi d0e93a
+touch %{buildroot}/opt/file
Florian Festi d0e93a
+
Florian Festi d0e93a
+%files
Florian Festi d0e93a
+/opt/file
Florian Festi d0e93a
+
Florian Festi d0e93a
+%changelog
Florian Festi d0e93a
diff --git a/tests/rpmbuild.at b/tests/rpmbuild.at
Florian Festi d0e93a
index 30d8e6895d..f378a4af2a 100644
Florian Festi d0e93a
--- a/tests/rpmbuild.at
Florian Festi d0e93a
+++ b/tests/rpmbuild.at
Florian Festi d0e93a
@@ -1801,3 +1801,76 @@ runroot rpmbuild -ba --quiet      \
Florian Festi d0e93a
 [],
Florian Festi d0e93a
 [])
Florian Festi d0e93a
 AT_CLEANUP
Florian Festi d0e93a
+
Florian Festi d0e93a
+AT_SETUP([bcond macro])
Florian Festi d0e93a
+AT_KEYWORDS([bcond build])
Florian Festi d0e93a
+RPMDB_INIT
Florian Festi d0e93a
+
Florian Festi d0e93a
+# basic bcond behavior with --eval
Florian Festi d0e93a
+AT_CHECK([
Florian Festi d0e93a
+runroot rpm \
Florian Festi d0e93a
+	--eval "%bcond normally_on 1" \
Florian Festi d0e93a
+	--eval "%bcond normally_off 0" \
Florian Festi d0e93a
+	--eval "%bcond both_features %[[%{with normally_on} && %{with normally_off}]]" \
Florian Festi d0e93a
+	--eval "%{with normally_on}" \
Florian Festi d0e93a
+	--eval "%{with normally_off}" \
Florian Festi d0e93a
+	--eval "%{with both_features}"
Florian Festi d0e93a
+],
Florian Festi d0e93a
+[0],
Florian Festi d0e93a
+[
Florian Festi d0e93a
+
Florian Festi d0e93a
+
Florian Festi d0e93a
+1
Florian Festi d0e93a
+0
Florian Festi d0e93a
+0
Florian Festi d0e93a
+],
Florian Festi d0e93a
+[])
Florian Festi d0e93a
+
Florian Festi d0e93a
+# bcond behavior, without CLI options
Florian Festi d0e93a
+AT_CHECK([
Florian Festi d0e93a
+runroot rpmbuild -bb --quiet /data/SPECS/bcondtest.spec
Florian Festi d0e93a
+runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
Florian Festi d0e93a
+    grep has_bcond | sort
Florian Festi d0e93a
+],
Florian Festi d0e93a
+[0],
Florian Festi d0e93a
+[has_bcond(normally_on)
Florian Festi d0e93a
+],
Florian Festi d0e93a
+[])
Florian Festi d0e93a
+
Florian Festi d0e93a
+# bcond behavior, --with
Florian Festi d0e93a
+AT_CHECK([
Florian Festi d0e93a
+runroot rpmbuild -bb --quiet --with normally_on --with normally_off \
Florian Festi d0e93a
+    /data/SPECS/bcondtest.spec
Florian Festi d0e93a
+runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
Florian Festi d0e93a
+    grep has_bcond | sort
Florian Festi d0e93a
+],
Florian Festi d0e93a
+[0],
Florian Festi d0e93a
+[has_bcond(both_features)
Florian Festi d0e93a
+has_bcond(normally_off)
Florian Festi d0e93a
+has_bcond(normally_on)
Florian Festi d0e93a
+],
Florian Festi d0e93a
+[])
Florian Festi d0e93a
+
Florian Festi d0e93a
+# bcond behavior, --without
Florian Festi d0e93a
+AT_CHECK([
Florian Festi d0e93a
+runroot rpmbuild -bb --quiet --without normally_on --without normally_off \
Florian Festi d0e93a
+    /data/SPECS/bcondtest.spec
Florian Festi d0e93a
+runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
Florian Festi d0e93a
+    grep has_bcond | sort
Florian Festi d0e93a
+],
Florian Festi d0e93a
+[0],
Florian Festi d0e93a
+[],
Florian Festi d0e93a
+[])
Florian Festi d0e93a
+
Florian Festi d0e93a
+# bcond behavior, CLI overriding a complex defailt
Florian Festi d0e93a
+AT_CHECK([
Florian Festi d0e93a
+runroot rpmbuild -bb --quiet --with both_features /data/SPECS/bcondtest.spec
Florian Festi d0e93a
+runroot rpm -q --provides -p /build/RPMS/noarch/bcondtest-1.0-1.noarch.rpm |
Florian Festi d0e93a
+    grep has_bcond | sort
Florian Festi d0e93a
+],
Florian Festi d0e93a
+[0],
Florian Festi d0e93a
+[has_bcond(both_features)
Florian Festi d0e93a
+has_bcond(normally_on)
Florian Festi d0e93a
+],
Florian Festi d0e93a
+[])
Florian Festi d0e93a
+AT_CLEANUP