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