File 0001-Fix-a-buildroot-regression-on-an-early-__spec_instal.patch of Package rpm

From b20c731a5e916649b0257875febe260b5dfe3d72 Mon Sep 17 00:00:00 2001
Message-ID: <b20c731a5e916649b0257875febe260b5dfe3d72.1717146759.git.pmatilai@redhat.com>
From: Panu Matilainen <pmatilai@redhat.com>
Date: Fri, 31 May 2024 11:44:39 +0300
Subject: [PATCH] Fix a %buildroot regression on an early %__spec_install_pre
 %global override

The Fedora kernel.spec overrides %__spec_install_pre with this very
early in the spec:

	%global __spec_install_pre %{___build_pre}

This happens long before Name and all that is defined, so half of
the environment setup in %___build_pre just falls through with
unexpanded macros as the values (eg RPM_PACKAGE_NAME=%{NAME}) at this
stage, and get their real values on the re-expansion before the actual
scriptlet execution. But, due to pre-historical reasons, RPM_BUILD_ROOT
setting is conditional, depending on whether %buildroot is defined or
not. It used to be defined in the main macros file, so RPM_BUILD_ROOT
would also get that template-style value at this point in the spec,
but since 9d35c8df497534e1fbd806a4dc78802bcf35d7cb it no longer is.
And that causes RPM_BUILD_ROOT to be *dropped* from the pre-environment
"template" at this early stage of spec parsing.

Add a simple reproducer test, this level of meddling in packages is of
playing with fire and we may not be able to guarantee it always stays
this way, but at least we'll be aware of possible changes in behavior.

Commit 0a4e182e4003be1497452658b7b3327975694641 from 1998 is the first
macro version of this conditional that came back to bite us. It
should've been removed in 9d35c8df497534e1fbd806a4dc78802bcf35d7cb.

Reported in https://bugzilla.redhat.com/show_bug.cgi?id=2284036
---
 macros.in                          |  4 ++--
 tests/data/SPECS/install-hack.spec | 20 ++++++++++++++++++++
 tests/rpmbuild.at                  | 12 ++++++++++++
 3 files changed, 34 insertions(+), 2 deletions(-)
 create mode 100644 tests/data/SPECS/install-hack.spec

diff --git a/macros.in b/macros.in
index ba661d80a..44af70288 100644
--- a/macros.in
+++ b/macros.in
@@ -770,8 +770,8 @@ Supplements:   (%{name} = %{version}-%{release} and langpacks-%{1})\
   unset CDPATH DISPLAY ||:\
   unset DEBUGINFOD_URLS ||:\
   unset RPM_CONFIG_DIR ||:\
-  %{?buildroot:RPM_BUILD_ROOT=\"%{buildroot}\"\
-  export RPM_BUILD_ROOT}\
+  RPM_BUILD_ROOT=\"%{buildroot}\"\
+  export RPM_BUILD_ROOT\
   %{?_javaclasspath:CLASSPATH=\"%{_javaclasspath}\"\
   export CLASSPATH}\
   PKG_CONFIG_PATH=\"${PKG_CONFIG_PATH}:%{_libdir}/pkgconfig:%{_datadir}/pkgconfig\"\
diff --git a/tests/data/SPECS/install-hack.spec b/tests/data/SPECS/install-hack.spec
new file mode 100644
index 000000000..42ecbed18
--- /dev/null
+++ b/tests/data/SPECS/install-hack.spec
@@ -0,0 +1,20 @@
+# Mimic what fedora kernel.spec does. Whether this is a legitimate
+# thing for a package to or not is another question, but at least we'll
+# be aware if the behavior changes.
+%global __spec_install_pre %{___build_pre}
+
+Summary: install scriptlet section override hack
+Name: install-hack
+Version: 1.0
+Release: 1
+License: GPL
+Buildarch: noarch
+
+%description
+%{summary}
+
+%install
+mkdir -p $RPM_BUILD_ROOT/usr/local/bin
+
+%files
+/usr/local/bin/
diff --git a/tests/rpmbuild.at b/tests/rpmbuild.at
index 38fe476a3..a77ceeca4 100644
--- a/tests/rpmbuild.at
+++ b/tests/rpmbuild.at
@@ -3277,3 +3277,15 @@ BUILD_POST noarch noarch noarch
 ],
 [])
 RPMTEST_CLEANUP
+
+AT_SETUP([rpmbuild install_pre override])
+AT_KEYWORDS([build])
+RPMDB_INIT
+RPMTEST_CHECK([
+runroot rpmbuild -bb /data/SPECS/install-hack.spec
+],
+[0],
+[ignore],
+[ignore])
+RPMTEST_CLEANUP
+
-- 
2.45.1

openSUSE Build Service is sponsored by