From 97204c7b8b14db1ddf4099ca48fc8717d05e7d6a Mon Sep 17 00:00:00 2001
From: Nico <github@nicoboehr.de>
Date: Thu, 1 May 2025 07:20:18 +0000
Subject: [PATCH] add patches for fix parallel build issue with perl

see https://github.com/openwrt/packages/pull/26398
---
 ...inc-dot.patch-with-smaller-scope-fix.patch |  89 ++++++
 ...l-drop-110-always_use_miniperl.patch.patch | 257 ++++++++++++++++++
 ...build-race-condition-in-target-build.patch | 142 ++++++++++
 3 files changed, 488 insertions(+)
 create mode 100644 patches/packages/packages/0003-perl-replace-910-miniperl-needs-inc-dot.patch-with-smaller-scope-fix.patch
 create mode 100644 patches/packages/packages/0004-perl-drop-110-always_use_miniperl.patch.patch
 create mode 100644 patches/packages/packages/0005-perl-fix-parallel-build-race-condition-in-target-build.patch

diff --git a/patches/packages/packages/0003-perl-replace-910-miniperl-needs-inc-dot.patch-with-smaller-scope-fix.patch b/patches/packages/packages/0003-perl-replace-910-miniperl-needs-inc-dot.patch-with-smaller-scope-fix.patch
new file mode 100644
index 000000000..d650fd97e
--- /dev/null
+++ b/patches/packages/packages/0003-perl-replace-910-miniperl-needs-inc-dot.patch-with-smaller-scope-fix.patch
@@ -0,0 +1,89 @@
+From: Matthias Schiffer <mschiffer@universe-factory.net>
+Date: Fri, 25 Apr 2025 02:06:17 +0200
+Subject: perl: replace 910-miniperl-needs-inc-dot.patch with smaller scope fix
+
+The patch was first introduced in commit 4a94479f9652 ("perl: update to
+5.26.1") to fix the target build when the host perl has
+default_inc_excludes_dot enabled. It just added back the `-I`. to every
+call of miniperl; this solution is questionable however, as it adds `.` to
+the beginning of the search path, not as a final fallback like perl did
+before default_inc_excludes_dot (and like miniperl does).
+
+It is also not necessary - only two scripts, write_buildcustomize.pl and
+configpm, expect to be able to include a file from `.` (in both cases a
+file the script just generated). Just fix the two scripts instead.
+
+Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
+
+diff --git a/lang/perl/patches/910-fix-default_inc_excludes_dot.patch b/lang/perl/patches/910-fix-default_inc_excludes_dot.patch
+new file mode 100644
+index 0000000000000000000000000000000000000000..ab3e6243bda314f137d1c328fbbf0e7f3bb34f36
+--- /dev/null
++++ b/lang/perl/patches/910-fix-default_inc_excludes_dot.patch
+@@ -0,0 +1,22 @@
++--- a/write_buildcustomize.pl
+++++ b/write_buildcustomize.pl
++@@ -3,7 +3,7 @@
++ use strict;
++ 
++ my $osname = $^O;
++-my $file = 'lib/buildcustomize.pl';
+++my $file = './lib/buildcustomize.pl';
++ 
++ if ( @ARGV % 2 ) {
++     my $dir = shift;
++--- a/configpm
+++++ b/configpm
++@@ -129,7 +129,7 @@ if ($Opts{chdir}) {
++ my ($Config_SH, $Config_PM, $Config_heavy, $Config_POD);
++ my $Glossary = 'Porting/Glossary';
++ 
++-$Config_PM = "lib/Config.pm";
+++$Config_PM = "./lib/Config.pm";
++ $Config_POD = "lib/Config.pod";
++ $Config_SH = "config.sh";
++ 
+diff --git a/lang/perl/patches/910-miniperl-needs-inc-dot.patch b/lang/perl/patches/910-miniperl-needs-inc-dot.patch
+deleted file mode 100644
+index 6997f04d575e5bb92e78052cf693ae39bb464a20..0000000000000000000000000000000000000000
+--- a/lang/perl/patches/910-miniperl-needs-inc-dot.patch
++++ /dev/null
+@@ -1,38 +0,0 @@
+---- a/Makefile.SH
+-+++ b/Makefile.SH
+-@@ -346,7 +346,7 @@ OBJ_EXT = $_o
+- # Macros to invoke a copy of miniperl during the build.  Targets which
+- # are built using these macros should depend on \$(MINIPERL_EXE)
+- MINIPERL_EXE = miniperl\$(EXE_EXT)
+--MINIPERL = \$(LDLIBPTH) ./miniperl\$(EXE_EXT) -Ilib
+-+MINIPERL = \$(LDLIBPTH) ./miniperl\$(EXE_EXT) -Ilib -I.
+- 
+- # Macros to invoke sort the MANIFEST during build
+- MANIFEST_SRT = MANIFEST.srt
+-@@ -1001,7 +1001,7 @@ lib/buildcustomize.pl: $& $(miniperl_obj
+- 	@$(RMS) miniperl.xok
+- 	$(CC) $(CLDFLAGS) $(NAMESPACEFLAGS) -o $(MINIPERL_EXE) \
+- 	    $(miniperl_objs) $(libs)
+--	$(LDLIBPTH) ./miniperl$(HOST_EXE_EXT) -w -Ilib -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+-+	$(LDLIBPTH) ./miniperl$(HOST_EXE_EXT) -w -Ilib -I. -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+- 	$(MINIPERL) -f write_buildcustomize.pl
+- !NO!SUBS!
+- 		;;
+-@@ -1012,7 +1012,7 @@ lib/buildcustomize.pl: \$& \$(miniperl_d
+- 	@\$(RMS) miniperl.xok
+- 	@\$(RMS) \$(MINIPERL_EXE)
+- 	\$(LNS) \$(HOST_PERL) \$(MINIPERL_EXE)
+--	\$(LDLIBPTH) ./miniperl\$(HOST_EXE_EXT) -w -Ilib -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+-+	\$(LDLIBPTH) ./miniperl\$(HOST_EXE_EXT) -w -Ilib -I. -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+- 	\$(MINIPERL) -f write_buildcustomize.pl 'osname' "$osname"
+- !GROK!THIS!
+- 		else
+-@@ -1021,7 +1021,7 @@ lib/buildcustomize.pl: $& $(miniperl_dep
+- 	@$(RMS) miniperl.xok
+- 	$(CC) $(CLDFLAGS) -o $(MINIPERL_EXE) \
+- 	    $(miniperl_objs) $(libs)
+--	$(LDLIBPTH) ./miniperl$(HOST_EXE_EXT) -w -Ilib -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+-+	$(LDLIBPTH) ./miniperl$(HOST_EXE_EXT) -w -Ilib -I. -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+- 	$(MINIPERL) -f write_buildcustomize.pl
+- !NO!SUBS!
+- 		fi
diff --git a/patches/packages/packages/0004-perl-drop-110-always_use_miniperl.patch.patch b/patches/packages/packages/0004-perl-drop-110-always_use_miniperl.patch.patch
new file mode 100644
index 000000000..2ae5afd19
--- /dev/null
+++ b/patches/packages/packages/0004-perl-drop-110-always_use_miniperl.patch.patch
@@ -0,0 +1,257 @@
+From: Matthias Schiffer <mschiffer@universe-factory.net>
+Date: Fri, 25 Apr 2025 12:35:23 +0200
+Subject: perl: drop 110-always_use_miniperl.patch
+
+The patch was introduced in commit 4c57844f0f04 ("lang/perl: Add hack to
+make perl always use miniperl during build"), but it is not actually
+necessary. By setting $perl to a non-empty value (using 'perl' as is
+common on desktop distros), the logic works as intended and selects the
+correct perl binary for host and target builds.
+
+As miniperl just symlinks to host perl for target builds, the main
+effect of this change is not unconditionally passing `-Ilib -I.`
+anymore. This seems like a good thing; host libraries should be used
+with host perl by default.
+
+Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
+
+diff --git a/lang/perl/files/base.config b/lang/perl/files/base.config
+index 67232b6d83e70505e7e85ad6893a2661f4571f7f..7d8b88b200f589b0efad7b301d26c6f9b77dfb33 100644
+--- a/lang/perl/files/base.config
++++ b/lang/perl/files/base.config
+@@ -864,7 +864,7 @@ package='perl5'
+ pager='/usr/bin/less'
+ passcat='cat /etc/passwd'
+ path_sep=':'
+-perl=''
++perl='perl'
+ perl5=''
+ perl_patchlevel=''
+ perl_static_inline='static __inline__'
+diff --git a/lang/perl/patches/110-always_use_miniperl.patch b/lang/perl/patches/110-always_use_miniperl.patch
+deleted file mode 100644
+index 806a31256b2007ee5f2c36d75261fac0d176cc21..0000000000000000000000000000000000000000
+--- a/lang/perl/patches/110-always_use_miniperl.patch
++++ /dev/null
+@@ -1,27 +0,0 @@
+---- a/Makefile.SH
+-+++ b/Makefile.SH
+-@@ -360,22 +360,11 @@ PERL_EXE_LDFLAGS=$exeldflags
+- 	;;
+- esac
+- 
+--case "$usecrosscompile$perl" in
+--define?*)
+--	$spitshell >>$Makefile <<!GROK!THIS!
+--# Macros to invoke a copy of our fully operational perl during the build.
+--PERL_EXE = perl\$(EXE_EXT)
+--RUN_PERL = \$(LDLIBPTH) \$(RUN) $perl\$(EXE_EXT)
+--!GROK!THIS!
+--	;;
+--*)
+--	$spitshell >>$Makefile <<!GROK!THIS!
+-+$spitshell >>$Makefile <<!GROK!THIS!
+- # Macros to invoke a copy of our fully operational perl during the build.
+- PERL_EXE = perl\$(EXE_EXT)
+--RUN_PERL = \$(LDLIBPTH) \$(RUN) ./perl\$(EXE_EXT) -Ilib -I.
+-+RUN_PERL = \$(LDLIBPTH) \$(RUN) ./miniperl\$(EXE_EXT) -Ilib -I.
+- !GROK!THIS!
+--	;;
+--esac
+- 
+- $spitshell >>$Makefile <<!GROK!THIS!
+- # Macros to run our tests
+diff --git a/lang/perl/patches/900-use-rm-force.patch b/lang/perl/patches/900-use-rm-force.patch
+index 9e44f5402f184445dcf4e9c75d96d2e412e5c4e0..baff506f6427f0b16712c252b0159ad80db20243 100644
+--- a/lang/perl/patches/900-use-rm-force.patch
++++ b/lang/perl/patches/900-use-rm-force.patch
+@@ -8,7 +8,7 @@
+  ranlib = $ranlib
+  ECHO = $echo
+  
+-@@ -791,7 +792,7 @@ bitcount.h: generate_uudmap$(HOST_EXE_EX
++@@ -802,7 +803,7 @@ bitcount.h: generate_uudmap$(HOST_EXE_EX
+  	./generate_uudmap$(HOST_EXE_EXT) $(generated_headers)
+  
+  generate_uudmap$(HOST_EXE_EXT): generate_uudmap$(OBJ_EXT)
+@@ -17,7 +17,7 @@
+  	$(LNS) $(HOST_GENERATE) generate_uudmap$(HOST_EXE_EXT)
+  
+  !NO!SUBS!
+-@@ -896,26 +897,26 @@ mydtrace.h: $(DTRACE_H)
++@@ -907,26 +908,26 @@ mydtrace.h: $(DTRACE_H)
+  	define)
+  		$spitshell >>$Makefile <<'!NO!SUBS!'
+  $(DTRACE_MINI_O): perldtrace.d $(miniperl_objs_nodt)
+@@ -48,7 +48,7 @@
+  
+  !NO!SUBS!
+  		;;
+-@@ -926,13 +927,13 @@ $(LIBPERL): $& $(perllib_dep) $(DYNALOAD
++@@ -937,13 +938,13 @@ $(LIBPERL): $& $(perllib_dep) $(DYNALOAD
+  	case "$useshrplib" in
+  	true)
+  		$spitshell >>$Makefile <<'!NO!SUBS!'
+@@ -64,7 +64,7 @@
+  	mv $@ libperl$(OBJ_EXT)
+  	$(AR) qv $(LIBPERL) libperl$(OBJ_EXT)
+  !NO!SUBS!
+-@@ -941,7 +942,7 @@ $(LIBPERL): $& $(perllib_dep) $(DYNALOAD
++@@ -952,7 +953,7 @@ $(LIBPERL): $& $(perllib_dep) $(DYNALOAD
+  		;;
+  	*)
+  		$spitshell >>$Makefile <<'!NO!SUBS!'
+@@ -73,7 +73,7 @@
+  	$(AR) rc $(LIBPERL) $(perllib_objs) $(DYNALOADER)
+  	@$(ranlib) $(LIBPERL)
+  !NO!SUBS!
+-@@ -975,7 +976,7 @@ $(MINIPERL_EXE): lib/buildcustomize.pl
++@@ -986,7 +987,7 @@ $(MINIPERL_EXE): lib/buildcustomize.pl
+  	amigaos*)
+  		$spitshell >>$Makefile <<'!NO!SUBS!'
+  lib/buildcustomize.pl: $& $(miniperl_objs) write_buildcustomize.pl
+@@ -82,7 +82,7 @@
+  	$(CC) $(CLDFLAGS) -o $(MINIPERL_EXE) \
+  	    $(miniperl_objs) $(libs)
+  #	$(LDLIBPTH) ./miniperl$(HOST_EXE_EXT) -w -Ilib -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+-@@ -997,7 +998,7 @@ NAMESPACEFLAGS = -force_flat_namespace
++@@ -1008,7 +1009,7 @@ NAMESPACEFLAGS = -force_flat_namespace
+  		esac
+  		$spitshell >>$Makefile <<'!NO!SUBS!'
+  lib/buildcustomize.pl: $& $(miniperl_objs) write_buildcustomize.pl
+@@ -91,7 +91,7 @@
+  	$(CC) $(CLDFLAGS) $(NAMESPACEFLAGS) -o $(MINIPERL_EXE) \
+  	    $(miniperl_objs) $(libs)
+  	$(LDLIBPTH) ./miniperl$(HOST_EXE_EXT) -w -Ilib -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+-@@ -1008,8 +1009,8 @@ lib/buildcustomize.pl: $& $(miniperl_obj
++@@ -1019,8 +1020,8 @@ lib/buildcustomize.pl: $& $(miniperl_obj
+  		if test "X$hostperl" != X; then
+  			$spitshell >>$Makefile <<!GROK!THIS!
+  lib/buildcustomize.pl: \$& \$(miniperl_dep) write_buildcustomize.pl
+@@ -102,7 +102,7 @@
+  	\$(LNS) \$(HOST_PERL) \$(MINIPERL_EXE)
+  	\$(LDLIBPTH) ./miniperl\$(HOST_EXE_EXT) -w -Ilib -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+  	\$(MINIPERL) -f write_buildcustomize.pl 'osname' "$osname"
+-@@ -1017,7 +1018,7 @@ lib/buildcustomize.pl: \$& \$(miniperl_d
++@@ -1028,7 +1029,7 @@ lib/buildcustomize.pl: \$& \$(miniperl_d
+  		else
+  			$spitshell >>$Makefile <<'!NO!SUBS!'
+  lib/buildcustomize.pl: $& $(miniperl_dep) write_buildcustomize.pl
+@@ -111,7 +111,7 @@
+  	$(CC) $(CLDFLAGS) -o $(MINIPERL_EXE) \
+  	    $(miniperl_objs) $(libs)
+  	$(LDLIBPTH) ./miniperl$(HOST_EXE_EXT) -w -Ilib -Idist/Exporter/lib -MExporter -e '<?>' || sh -c 'echo >&2 Failed to build miniperl.  Please run make minitest; exit 1'
+-@@ -1030,7 +1031,7 @@ lib/buildcustomize.pl: $& $(miniperl_dep
++@@ -1041,7 +1042,7 @@ lib/buildcustomize.pl: $& $(miniperl_dep
+  	$spitshell >>$Makefile <<'!NO!SUBS!'
+  
+  $(PERL_EXE): $& $(perlmain_dep) $(LIBPERL) $(static_ext) ext.libs $(PERLEXPORT) write_buildcustomize.pl
+@@ -120,7 +120,7 @@
+  !NO!SUBS!
+  
+          case "$osname" in
+-@@ -1130,8 +1131,8 @@ pod/perl5400delta.pod: pod/perldelta.pod
++@@ -1141,8 +1142,8 @@ pod/perl5400delta.pod: pod/perldelta.pod
+  	$(LNS) perldelta.pod pod/perl5400delta.pod
+  
+  extra.pods: $(MINIPERL_EXE)
+@@ -131,7 +131,7 @@
+  	-@for x in `grep -l '^=[a-z]' README.* | grep -v README.vms` ; do \
+  	    nx=`echo $$x | sed -e "s/README\.//"`; \
+  	    $(LNS) ../$$x "pod/perl"$$nx".pod" ; \
+-@@ -1330,11 +1331,11 @@ realclean:	_realcleaner _mopup
++@@ -1341,11 +1342,11 @@ realclean:	_realcleaner _mopup
+  	@echo "Note that '$(MAKE) realclean' does not delete config.sh or Policy.sh"
+  
+  _clobber:
+@@ -148,7 +148,7 @@
+  
+  clobber:	_realcleaner _mopup _clobber
+  
+-@@ -1342,24 +1343,24 @@ distclean:	clobber
++@@ -1353,24 +1354,24 @@ distclean:	clobber
+  
+  # Like distclean but also removes emacs backups and *.orig.
+  veryclean:	_verycleaner _mopup _clobber
+@@ -184,7 +184,7 @@
+  	-cd pod; $(LDLIBPTH) $(MAKE) $(CLEAN)
+  	-cd utils; $(LDLIBPTH) $(MAKE) $(CLEAN)
+  	-@if test -f $(MINIPERL_EXE) ; then \
+-@@ -1369,8 +1370,8 @@ _cleaner1:
++@@ -1380,8 +1381,8 @@ _cleaner1:
+  	else \
+  	sh $(CLEAN).sh ; \
+  	fi
+@@ -195,7 +195,7 @@
+  
+  # Dear POSIX, thanks for making the default to xargs to be
+  # run once if nothing is passed in. It is such a great help.
+-@@ -1385,24 +1386,24 @@ _cleaner1:
++@@ -1396,24 +1397,24 @@ _cleaner1:
+  # Add new rules before that line - the next line (rm -f so_locations ...) is
+  # used as a placeholder by a regen script.
+  _cleaner2:
+@@ -237,7 +237,7 @@
+  	-rmdir lib/version lib/threads lib/inc/ExtUtils lib/inc lib/encoding
+  	-rmdir lib/autodie/exception lib/autodie/Scope lib/autodie lib/XS
+  	-rmdir lib/Win32API lib/VMS lib/Unicode/Collate/Locale
+-@@ -1459,11 +1460,11 @@ _realcleaner:
++@@ -1470,11 +1471,11 @@ _realcleaner:
+  _verycleaner:
+  	@$(LDLIBPTH) $(MAKE) _cleaner1 CLEAN=veryclean
+  	@$(LDLIBPTH) $(MAKE) _cleaner2
+@@ -251,7 +251,7 @@
+  	lint $(lintflags) -DPERL_CORE -D_REENTRANT -DDEBUGGING -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 $(c)
+  
+  cscopeflags = -Rb  # Recursive, build-only.
+-@@ -1524,7 +1525,7 @@ case "$targethost" in
++@@ -1535,7 +1536,7 @@ case "$targethost" in
+  '') $spitshell >>$Makefile <<'!NO!SUBS!'
+  test_prep test-prep: test_prep_pre $(MINIPERL_EXE) $(unidatafiles) $(PERL_EXE) \
+  	$(dynamic_ext) $(TEST_PERL_DLL) runtests $(generated_pods) common_build
+@@ -260,7 +260,7 @@
+  
+  !NO!SUBS!
+  ;;
+-@@ -1574,7 +1575,7 @@ test_prep test-prep: test_prep_pre \$(MI
++@@ -1585,7 +1586,7 @@ test_prep test-prep: test_prep_pre \$(MI
+  	$to config.sh
+  # --- For lib/diagnostics.t with -Duseshrplib
+  	$to \$(PERL_EXE)
+@@ -269,7 +269,7 @@
+  	$to t/\$(PERL_EXE)
+  
+  !GROK!THIS!
+-@@ -1592,7 +1593,7 @@ else
++@@ -1603,7 +1604,7 @@ else
+  $spitshell >>$Makefile <<'!NO!SUBS!'
+  test_prep_reonly: $(MINIPERL_EXE) $(PERL_EXE) $(dynamic_ext_re) $(TEST_PERL_DLL)
+  	$(MINIPERL) make_ext.pl $(dynamic_ext_re) MAKE="$(MAKE)" LIBPERL_A=$(LIBPERL) LINKTYPE=dynamic
+@@ -278,7 +278,7 @@
+  !NO!SUBS!
+  fi
+  
+-@@ -1648,7 +1649,7 @@ minitest_prep: $(MINIPERL_EXE)
++@@ -1659,7 +1660,7 @@ minitest_prep: $(MINIPERL_EXE)
+  	@echo "You may see some irrelevant test failures if you have been unable"
+  	@echo "to build lib/Config.pm, or the Unicode data files."
+  	@echo " "
+diff --git a/lang/perl/patches/920-Revert-perl-127606-adjust-dependency-paths-on-instal.patch b/lang/perl/patches/920-Revert-perl-127606-adjust-dependency-paths-on-instal.patch
+index e9e5688e475e810f45acbda5cd579c65e484b8b7..6436a8ea027f46a8ce1c41b13323b625cc51eb4d 100644
+--- a/lang/perl/patches/920-Revert-perl-127606-adjust-dependency-paths-on-instal.patch
++++ b/lang/perl/patches/920-Revert-perl-127606-adjust-dependency-paths-on-instal.patch
+@@ -42,10 +42,10 @@ Signed-off-by: Georgi Valkov <gvalkov@gmail.com>
+ -	;;
+ -esac
+ -
+- $spitshell >>$Makefile <<!GROK!THIS!
+- # Macros to invoke a copy of our fully operational perl during the build.
+- PERL_EXE = perl\$(EXE_EXT)
+-@@ -1045,20 +1029,6 @@ $(PERL_EXE): $& $(perlmain_dep) $(LIBPER
++ case "$usecrosscompile$perl" in
++ define?*)
++ 	$spitshell >>$Makefile <<!GROK!THIS!
++@@ -1056,20 +1040,6 @@ $(PERL_EXE): $& $(perlmain_dep) $(LIBPER
+  	$(SHRPENV) $(CC) -o perl $(CLDFLAGS) $(CCDLFLAGS) $(perlmain_objs) $(LLIBPERL) $(static_ext) `cat ext.libs` $(libs)
+  !NO!SUBS!
+          ;;
diff --git a/patches/packages/packages/0005-perl-fix-parallel-build-race-condition-in-target-build.patch b/patches/packages/packages/0005-perl-fix-parallel-build-race-condition-in-target-build.patch
new file mode 100644
index 000000000..5ca151193
--- /dev/null
+++ b/patches/packages/packages/0005-perl-fix-parallel-build-race-condition-in-target-build.patch
@@ -0,0 +1,142 @@
+From: Matthias Schiffer <mschiffer@universe-factory.net>
+Date: Sat, 26 Apr 2025 22:37:16 +0200
+Subject: perl: fix parallel build race condition in target build
+
+We have received reports of builds of perl occasionally failing when
+building with many parallel jobs, with a log like the following:
+
+    LD_LIBRARY_PATH=[...]/perl/perl-5.40.0 ./miniperl -Ilib make_ext.pl \
+        dist/constant/pm_to_blib  MAKE="make" LIBPERL_A=libperl.so
+    File/Path.pm did not return a true value at [...]/hostpkg/usr/lib/perl5/5.40.0/ExtUtils/MakeMaker.pm line 13.
+    BEGIN failed--compilation aborted at [...]/hostpkg/usr/lib/perl5/5.40.0/ExtUtils/MakeMaker.pm line 13.
+    Compilation failed in require at Makefile.PL line 3.
+    BEGIN failed--compilation aborted at Makefile.PL line 3.
+    Unsuccessful Makefile.PL(dist/constant): code=65280 at make_ext.pl line 532.
+
+The failing extension (dist/constant in the above log) would differ
+between runs.
+
+The cause of the issue is the `-Ilib` in the command line of miniperl.
+In the host build, `./miniperl -I lib` will use the following include
+path:
+
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/AutoLoader/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/dist/Carp/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/dist/PathTools
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/dist/PathTools/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/ExtUtils-Install/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/ExtUtils-MakeMaker/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/ExtUtils-Manifest/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/File-Path/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/ext/re
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/dist/Term-ReadLine/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/dist/Exporter/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/ext/File-Find/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/Text-Tabs/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/dist/constant/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/version/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/Getopt-Long/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/Text-ParseWords/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/cpan/ExtUtils-PL2Bat/lib
+    [..]/build_dir/hostpkg/perl/perl-5.40.0/lib
+    .
+
+Various dependencies of the extension build scripts (Makefile.PL) -
+including File-Path, which failed to be loaded in the error log - are
+included in the path by buildcustomize.pl, as these extensions are only
+installed to `lib` as the build proceeds.
+
+However, in a target build, miniperl is just a symlink to the previously
+built host perl. As the host perl does not implicitly load
+`buildcustomize.pl`, we get the following include path for
+`./miniperl -Ilib`:
+
+    lib
+    [..]/staging_dir/hostpkg/usr/lib/perl5/site_perl/5.40.0/x86_64-linux
+    [..]/staging_dir/hostpkg/usr/lib/perl5/site_perl/5.40.0
+    [..]/staging_dir/hostpkg/usr/lib/perl5/5.40.0/x86_64-linux
+    [..]/staging_dir/hostpkg/usr/lib/perl5/5.40.0
+
+The host perl's install location is used as the default include path
+which provides File-Path etc. for the target build; however, as more
+and more libraries get installed into `lib` during the extension build,
+they may get loaded from there instead, as `lib` is at the beginning of
+the include path. When multiple extensions are built in parallel, a
+Makefile.PL may attempt to load File/Path from `lib` after the file has
+been created, but before its contents have been written fully, resulting
+in the build to fail.
+
+In fact, we should not load anything from `lib` during the target build,
+as it is the staging directory for the target, including native
+extensions built for the target architecture - with one exception: The
+build scripts expect to find target information in the `Config` module,
+so simply removing `lib` from the include path completely would break
+the build.
+
+Solve the issue by creating an alternative lib directory `lib_build`,
+symlinking `Config.pm` and its dependencies in it, and replacing the
+`-Ilib` argument with `-Ilib_build` using a wrapper script around the
+host perl executable. This is similar to the approach seen in perl's own
+obsolete/broken cross compile scripts (`Cross/Makefile`).
+
+Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
+
+diff --git a/lang/perl/Makefile b/lang/perl/Makefile
+index 6a6dd5ea86798e7e95e3657a94cca829dbd0924b..6ad27818e17c211abc1530eb0d1b1e7c1238bf28 100644
+--- a/lang/perl/Makefile
++++ b/lang/perl/Makefile
+@@ -92,6 +92,21 @@ endef
+ 
+ # Target perl
+ define Build/Configure
++	# We don't want to pass -Ilib to host perl in the target build (as lib
++	# contains the target libraries, and files may currently be written
++	# while being imported in parallel builds). We do however need the
++	# target versions of the Config modules at the beginning of the include
++	# path for the build scripts' use.
++	#
++	# Create an alternative lib_build directory that will be added to the
++	# include path instead of lib (using hostperl-wrapper), containing only
++	# the config modules.
++	$(INSTALL_DIR) $(PKG_BUILD_DIR)/lib_build
++	ln -sf ../lib/Config.pm ../lib/Config_heavy.pl ../lib/Config_git.pl $(PKG_BUILD_DIR)/lib_build/
++
++	install -m0755 files/hostperl-wrapper $(PKG_BUILD_DIR)/hostperl-wrapper
++	sed -i "s'@HOST_PERL@'$(HOST_PERL_PREFIX)/bin/perl'g" $(PKG_BUILD_DIR)/hostperl-wrapper
++
+ 	$(PERL_CMD) files/perlconfig.pl -Dowrt:target_cc='$(TARGET_CC)' \
+ 	                                -Dowrt:gccversion=$(CONFIG_GCC_VERSION) \
+ 	                                -Dowrt:target_cross='$(TARGET_CROSS)' \
+diff --git a/lang/perl/files/base.config b/lang/perl/files/base.config
+index 7d8b88b200f589b0efad7b301d26c6f9b77dfb33..1cadfc10ac20c3b2bb05bed3c2cc8e946ba24eef 100644
+--- a/lang/perl/files/base.config
++++ b/lang/perl/files/base.config
+@@ -650,7 +650,7 @@ hint='recommended'
+ hostcat='cat /etc/hosts'
+ hostgenerate="$owrt:host_perl_prefix/bin/generate_uudmap"
+ hostosname=''
+-hostperl="$owrt:host_perl_prefix/bin/perl"
++hostperl="./hostperl-wrapper"
+ html1dir=' '
+ html1direxp=''
+ html3dir=' '
+diff --git a/lang/perl/files/hostperl-wrapper b/lang/perl/files/hostperl-wrapper
+new file mode 100644
+index 0000000000000000000000000000000000000000..e31ef1cf8fe37a7a90ee9dd7cc60d3ca947e7600
+--- /dev/null
++++ b/lang/perl/files/hostperl-wrapper
+@@ -0,0 +1,14 @@
++#!@HOST_PERL@
++
++foreach (@ARGV) {
++	# Stop parsing options if we encounter a non-option argument or --
++	last if $_ eq '--' || $_ !~ m/^-/;
++
++	# Modify first option of the form -Ilib, -I../lib, ... to refer to lib_build instead
++	if ($_ =~ m@-I(.*/)?lib@) {
++		$_ .= '_build';
++		last;
++	}
++}
++
++exec '@HOST_PERL@', @ARGV
-- 
GitLab