From 0a900c82c37998d41f4f8bcaff338a5aafeb6578 Mon Sep 17 00:00:00 2001
From: Benjamin Drung <benjamin.drung@canonical.com>
Date: Tue, 20 Jan 2026 13:56:58 +0100
Subject: [PATCH] Exclude all LDFLAGS from libR.pc

Indroduce `LIBR_LDFLAGS` environment variable to allow overwriting the
the LDFLAGS added to `libR.pc`. This allows distributions to curate
which linker flags should be added to the pkg-config file.

See also: https://bugs.r-project.org/show_bug.cgi?id=17744
Bug-Gentoo: https://bugs.gentoo.org/515428
Bug-Ubuntu: https://launchpad.net/bugs/2138363
---
 ...not-ignore-failure-to-create-libR.pc.patch | 32 ++++++++++++++++
 ...ce-LIBR_LDFLAGS-environment-variable.patch | 38 +++++++++++++++++++
 debian/patches/series                         |  2 +
 debian/rules                                  |  2 +-
 4 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 debian/patches/do-not-ignore-failure-to-create-libR.pc.patch
 create mode 100644 debian/patches/introduce-LIBR_LDFLAGS-environment-variable.patch

diff --git a/debian/patches/do-not-ignore-failure-to-create-libR.pc.patch b/debian/patches/do-not-ignore-failure-to-create-libR.pc.patch
new file mode 100644
index 000000000..0a5b10385
--- /dev/null
+++ b/debian/patches/do-not-ignore-failure-to-create-libR.pc.patch
@@ -0,0 +1,32 @@
+From: Benjamin Drung <benjamin.drung@canonical.com>
+Date: Tue, 20 Jan 2026 13:48:48 +0100
+Subject: do not ignore failure to create libR.pc
+
+Silently failing to create `libR.pc` will make debugging harder and is
+not what downstream distributions want.
+
+The sed command can fail in case one of the substitution strings
+contains the separator. Instead of ignoring the failure, a separator
+character should be picked that is not part of the substitution string.
+`LDFLAGS` might contain `:` and fail.
+
+Bug: https://bugs.r-project.org/show_bug.cgi?id=18993
+See also: https://bugs.r-project.org/show_bug.cgi?id=13946
+Forwarded: https://bugs.r-project.org/show_bug.cgi?id=18993
+---
+ src/unix/Makefile.in | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/src/unix/Makefile.in b/src/unix/Makefile.in
+index fc08a86..6d2e3e9 100644
+--- a/src/unix/Makefile.in
++++ b/src/unix/Makefile.in
+@@ -83,7 +83,7 @@ install-Rscript:
+ ## substitute at install time to allow 'prefix' to be overridden
+ install-pc:
+ 	@$(MKINSTALLDIRS) "$(DESTDIR)$(libdir)/pkgconfig"
+-	-@$(SED) -e "s:@rhome:$(rhome):" \
++	@$(SED) -e "s:@rhome:$(rhome):" \
+ 	  -e "s:@rincludedir:$(rincludedir):" \
+ 	  -e "s:@rarch:$(R_ARCH):" \
+ 	  -e 's:@libsprivate:$(STATIC_LIBR_PC):' \
diff --git a/debian/patches/introduce-LIBR_LDFLAGS-environment-variable.patch b/debian/patches/introduce-LIBR_LDFLAGS-environment-variable.patch
new file mode 100644
index 000000000..3ab0e4a8c
--- /dev/null
+++ b/debian/patches/introduce-LIBR_LDFLAGS-environment-variable.patch
@@ -0,0 +1,38 @@
+From: Benjamin Drung <benjamin.drung@canonical.com>
+Date: Fri, 16 Jan 2026 11:47:48 +0100
+Subject: introduce LIBR_LDFLAGS environment variable
+
+Indroduce `LIBR_LDFLAGS` environment variable to allow overwriting the
+the LDFLAGS added to `libR.pc`. This allows distributions to curate
+which linker flags should be added to the pkg-config file.
+
+Bug: https://bugs.r-project.org/show_bug.cgi?id=18992
+Bug-Gentoo: https://bugs.gentoo.org/515428
+Bug-Ubuntu: https://launchpad.net/bugs/2138363
+See also: https://bugs.r-project.org/show_bug.cgi?id=17744
+Forwarded: https://bugs.r-project.org/show_bug.cgi?id=18992
+---
+ src/unix/Makefile.in | 3 ++-
+ 1 file changed, 2 insertions(+), 1 deletion(-)
+
+diff --git a/src/unix/Makefile.in b/src/unix/Makefile.in
+index 2424c6a..fc08a86 100644
+--- a/src/unix/Makefile.in
++++ b/src/unix/Makefile.in
+@@ -13,6 +13,7 @@ include $(top_builddir)/Makeconf
+ LIBINTL= @BUILD_LIBINTL_FALSE@ @LIBINTL@
+ R_HOME=$(rhome)
+ STATIC_LIBR_PC = @WANT_R_STATIC_TRUE@ $(BLAS_LIBS) $(FLIBS) @R_XTRA_LIBS@ $(LIBINTL) @READLINE_LIBS@ $(LIBS)
++LIBR_LDFLAGS = $(LDFLAGS)
+ 
+ ALL_CPPFLAGS = $(R_XTRA_CPPFLAGS) $(X_CFLAGS) $(CPPFLAGS) $(DEFS)
+ 
+@@ -86,7 +87,7 @@ install-pc:
+ 	  -e "s:@rincludedir:$(rincludedir):" \
+ 	  -e "s:@rarch:$(R_ARCH):" \
+ 	  -e 's:@libsprivate:$(STATIC_LIBR_PC):' \
+-	  -e 's:@others:$(MAIN_LDFLAGS) $(LDFLAGS):' \
++	  -e 's:@others:$(MAIN_LDFLAGS) $(LIBR_LDFLAGS):' \
+ 	  -e 's/@VERSION/$(VERSION)/' $(srcdir)/libR.pc.in \
+ 	  > "$(DESTDIR)$(libdir)/pkgconfig/libR.pc"
+ 
diff --git a/debian/patches/series b/debian/patches/series
index 96f5143ba..aac6771a2 100644
--- a/debian/patches/series
+++ b/debian/patches/series
@@ -6,3 +6,5 @@
 set_R_LIBS_USER_and_SITE_in_Renviron.patch
 # r_4_4_x_tar_archive_symlink
 # libjvm.so-for-zero-build.patch
+introduce-LIBR_LDFLAGS-environment-variable.patch
+do-not-ignore-failure-to-create-libR.pc.patch
diff --git a/debian/rules b/debian/rules
index 23e740dc6..0e4b5ae64 100755
--- a/debian/rules
+++ b/debian/rules
@@ -449,7 +449,7 @@ install-arch-stamp:
 	dh_installdirs -A
 	@echo $(debtmp)
 
-	$(MAKE) DESTDIR=$(debtmp) install
+	$(MAKE) DESTDIR=$(debtmp) LIBR_LDFLAGS= install
 
         # edd 16 Apr 2008  make Rscript _again_ as the build is broken
         #                  and we need an explicit RHOME
-- 
2.51.0

