+sub patch_SH {
+ # Cwd.xs added in commit 0d2079faa739aaa9. Cwd.pm moved to ext/ 8 years
+ # later in commit 403f501d5b37ebf0
+ if ($major > 0 && <*/Cwd/Cwd.xs>) {
+ if ($major < 10
+ && !extract_from_file('Makefile.SH', qr/^extra_dep=''$/)) {
+ # The Makefile.PL for Unicode::Normalize needs
+ # lib/unicore/CombiningClass.pl. Even without a parallel build, we
+ # need a dependency to ensure that it builds. This is a variant of
+ # commit 9f3ef600c170f61e. Putting this for earlier versions gives
+ # us a spot on which to hang the edits below
+ apply_patch(<<'EOPATCH');
+diff --git a/Makefile.SH b/Makefile.SH
+index f61d0db..6097954 100644
+--- a/Makefile.SH
++++ b/Makefile.SH
+@@ -155,10 +155,20 @@ esac
+
+ : Prepare dependency lists for Makefile.
+ dynamic_list=' '
++extra_dep=''
+ for f in $dynamic_ext; do
+ : the dependency named here will never exist
+ base=`echo "$f" | sed 's/.*\///'`
+- dynamic_list="$dynamic_list lib/auto/$f/$base.$dlext"
++ this_target="lib/auto/$f/$base.$dlext"
++ dynamic_list="$dynamic_list $this_target"
++
++ : Parallel makes reveal that we have some interdependencies
++ case $f in
++ Math/BigInt/FastCalc) extra_dep="$extra_dep
++$this_target: lib/auto/List/Util/Util.$dlext" ;;
++ Unicode/Normalize) extra_dep="$extra_dep
++$this_target: lib/unicore/CombiningClass.pl" ;;
++ esac
+ done
+
+ static_list=' '
+@@ -987,2 +997,9 @@ n_dummy $(nonxs_ext): miniperl$(EXE_EXT) preplibrary $(DYNALOADER) FORCE
+ @$(LDLIBPTH) sh ext/util/make_ext nonxs $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
++!NO!SUBS!
++
++$spitshell >>Makefile <<EOF
++$extra_dep
++EOF
++
++$spitshell >>Makefile <<'!NO!SUBS!'
+
+EOPATCH
+ }
+
+ if ($major == 15 && $^O !~ /^(linux|darwin|.*bsd)$/
+ && extract_from_file('Makefile.SH', qr/^V.* \?= /)) {
+ # Remove the GNU-make-ism (which the BSD makes also support, but
+ # most other makes choke on)
+ apply_patch(<<'EOPATCH');
+diff --git a/Makefile.SH b/Makefile.SH
+index 94952bd..13e9001 100755
+--- a/Makefile.SH
++++ b/Makefile.SH
+@@ -338,8 +338,8 @@ linux*|darwin)
+ $spitshell >>$Makefile <<!GROK!THIS!
+ # If you're going to use valgrind and it can't be invoked as plain valgrind
+ # then you'll need to change this, or override it on the make command line.
+-VALGRIND ?= valgrind
+-VG_TEST ?= ./perl -e 1 2>/dev/null
++VALGRIND = valgrind
++VG_TEST = ./perl -e 1 2>/dev/null
+
+ !GROK!THIS!
+ ;;
+EOPATCH
+ }
+
+ if ($major == 11) {
+ if (extract_from_file('patchlevel.h',
+ qr/^#include "unpushed\.h"/)) {
+ # I had thought it easier to detect when building one of the 52
+ # commits with the original method of incorporating the git
+ # revision and drop parallel make flags. Commits shown by
+ # git log 46807d8e809cc127^..dcff826f70bf3f64^ ^d4fb0a1f15d1a1c4
+ # However, it's not actually possible to make miniperl for that
+ # configuration as-is, because the file .patchnum is only made
+ # as a side effect of target 'all'
+ # I also don't think that it's "safe" to simply run
+ # make_patchnum.sh before the build. We need the proper
+ # dependency rules in the Makefile to *stop* it being run again
+ # at the wrong time.
+ # This range is important because contains the commit that
+ # merges Schwern's y2038 work.
+ apply_patch(<<'EOPATCH');
+diff --git a/Makefile.SH b/Makefile.SH
+index 9ad8b6f..106e721 100644
+--- a/Makefile.SH
++++ b/Makefile.SH
+@@ -540,9 +544,14 @@ sperl.i: perl.c $(h)
+
+ .PHONY: all translators utilities make_patchnum
+
+-make_patchnum:
++make_patchnum: lib/Config_git.pl
++
++lib/Config_git.pl: make_patchnum.sh
+ sh $(shellflags) make_patchnum.sh
+
++# .patchnum, unpushed.h and lib/Config_git.pl are built by make_patchnum.sh
++unpushed.h .patchnum: lib/Config_git.pl
++
+ # make sure that we recompile perl.c if .patchnum changes
+ perl$(OBJ_EXT): .patchnum unpushed.h
+
+EOPATCH
+ } elsif (-f '.gitignore'
+ && extract_from_file('.gitignore', qr/^\.patchnum$/)) {
+ # 8565263ab8a47cda to 46807d8e809cc127^ inclusive.
+ edit_file('Makefile.SH', sub {
+ my $code = shift;
+ $code =~ s/^make_patchnum:\n/make_patchnum: .patchnum
+
+.sha1: .patchnum
+
+.patchnum: make_patchnum.sh
+/m;
+ return $code;
+ });
+ } elsif (-f 'lib/.gitignore'
+ && extract_from_file('lib/.gitignore',
+ qr!^/Config_git.pl!)
+ && !extract_from_file('Makefile.SH',
+ qr/^uudmap\.h.*:bitcount.h$/)) {
+ # Between commits and dcff826f70bf3f64 and 0f13ebd5d71f8177^
+ edit_file('Makefile.SH', sub {
+ my $code = shift;
+ # Bug introduced by 344af494c35a9f0f
+ # fixed in 0f13ebd5d71f8177
+ $code =~ s{^(pod/perlapi\.pod) (pod/perlintern\.pod): }
+ {$1: $2\n\n$2: }m;
+ # Bug introduced by efa50c51e3301a2c
+ # fixed in 0f13ebd5d71f8177
+ $code =~ s{^(uudmap\.h) (bitcount\.h): }
+ {$1: $2\n\n$2: }m;
+
+ # The rats nest of getting git_version.h correct
+
+ if ($code =~ s{git_version\.h: stock_git_version\.h
+\tcp stock_git_version\.h git_version\.h}
+ {}m) {
+ # before 486cd780047ff224
+
+ # We probably can't build between
+ # 953f6acfa20ec275^ and 8565263ab8a47cda
+ # inclusive, but all commits in that range
+ # relate to getting make_patchnum.sh working,
+ # so it is extremely unlikely to be an
+ # interesting bisect target. They will skip.
+
+ # No, don't spawn a submake if
+ # make_patchnum.sh or make_patchnum.pl fails
+ $code =~ s{\|\| \$\(MAKE\) miniperl.*}
+ {}m;
+ $code =~ s{^\t(sh.*make_patchnum\.sh.*)}
+ {\t-$1}m;
+
+ # Use an external perl to run make_patchnum.pl
+ # because miniperl still depends on
+ # git_version.h
+ $code =~ s{^\t.*make_patchnum\.pl}
+ {\t-$^X make_patchnum.pl}m;
+
+
+ # "Truth in advertising" - running
+ # make_patchnum generates 2 files.
+ $code =~ s{^make_patchnum:.*}{
+make_patchnum: lib/Config_git.pl
+
+git_version.h: lib/Config_git.pl
+
+perlmini\$(OBJ_EXT): git_version.h
+
+lib/Config_git.pl:}m;
+ }
+ # Right, now we've corrected Makefile.SH to
+ # correctly describe how lib/Config_git.pl and
+ # git_version.h are made, we need to fix the rest
+
+ # This emulates commit 2b63e250843b907e
+ # This might duplicate the rule stating that
+ # git_version.h depends on lib/Config_git.pl
+ # This is harmless.
+ $code =~ s{^(?:lib/Config_git\.pl )?git_version\.h: (.* make_patchnum\.pl.*)}
+ {git_version.h: lib/Config_git.pl
+
+lib/Config_git.pl: $1}m;
+
+ # This emulates commits 0f13ebd5d71f8177 and
+ # and a04d4598adc57886. It ensures that
+ # lib/Config_git.pl is built before configpm,
+ # and that configpm is run exactly once.
+ $code =~ s{^(\$\(.*?\) )?(\$\(CONFIGPOD\))(: .*? configpm Porting/Glossary)( lib/Config_git\.pl)?}{
+ # If present, other files depend on $(CONFIGPOD)
+ ($1 ? "$1: $2\n\n" : '')
+ # Then the rule we found
+ . $2 . $3
+ # Add dependency if not there
+ . ($4 ? $4 : ' lib/Config_git.pl')
+ }me;
+
+ return $code;
+ });
+ }
+ }
+
+ if ($major < 14) {
+ # Commits dc0655f797469c47 and d11a62fe01f2ecb2
+ edit_file('Makefile.SH', sub {
+ my $code = shift;
+ foreach my $ext (qw(Encode SDBM_File)) {
+ next if $code =~ /\b$ext\) extra_dep=/s;
+ $code =~ s!(\) extra_dep="\$extra_dep
+\$this_target: .*?" ;;)
+( esac
+)!$1
+ $ext) extra_dep="\$extra_dep
+\$this_target: lib/auto/Cwd/Cwd.\$dlext" ;;
+$2!;
+ }
+ return $code;
+ });
+ }
+ }
+
+ if ($major == 7) {
+ # Remove commits 9fec149bb652b6e9 and 5bab1179608f81d8, which add/amend
+ # rules to automatically run regen scripts that rebuild C headers. These
+ # cause problems because a git checkout doesn't preserve relative file
+ # modification times, hence the regen scripts may fire. This will
+ # obscure whether the repository had the correct generated headers
+ # checked in.
+ # Also, the dependency rules for running the scripts were not correct,
+ # which could cause spurious re-builds on re-running make, and can cause
+ # complete build failures for a parallel make.
+ if (extract_from_file('Makefile.SH',
+ qr/Writing it this way gives make a big hint to always run opcode\.pl before/)) {
+ apply_commit('70c6e6715e8fec53');
+ } elsif (extract_from_file('Makefile.SH',
+ qr/^opcode\.h opnames\.h pp_proto\.h pp\.sym: opcode\.pl$/)) {
+ revert_commit('9fec149bb652b6e9');
+ }
+ }
+
+ if ($^O eq 'aix' && $major >= 11 && $major <= 15
+ && extract_from_file('makedef.pl', qr/^use Config/)) {
+ edit_file('Makefile.SH', sub {
+ # The AIX part of commit e6807d8ab22b761c
+ # It's safe to substitute lib/Config.pm for config.sh
+ # as lib/Config.pm depends on config.sh
+ # If the tree is post e6807d8ab22b761c, the substitution
+ # won't match, which is harmless.
+ my $code = shift;
+ $code =~ s{^(perl\.exp:.* )config\.sh(\b.*)}
+ {$1 . '$(CONFIGPM)' . $2}me;
+ return $code;
+ });
+ }
+
+ # There was a bug in makedepend.SH which was fixed in version 96a8704c.
+ # Symptom was './makedepend: 1: Syntax error: Unterminated quoted string'
+ # Remove this if you're actually bisecting a problem related to
+ # makedepend.SH
+ # If you do this, you may need to add in code to correct the output of older
+ # makedepends, which don't correctly filter newer gcc output such as
+ # <built-in>
+ checkout_file('makedepend.SH');
+
+ if ($major < 4 && -f 'config.sh'
+ && !extract_from_file('config.sh', qr/^trnl=/)) {
+ # This seems to be necessary to avoid makedepend becoming confused,
+ # and hanging on stdin. Seems that the code after
+ # make shlist || ...here... is never run.
+ edit_file('makedepend.SH', sub {
+ my $code = shift;
+ $code =~ s/^trnl='\$trnl'$/trnl='\\n'/m;
+ return $code;
+ });
+ }
+}
+
+sub patch_C {
+ # This is ordered by $major, as it's likely that different platforms may
+ # well want to share code.
+
+ if ($major == 2 && extract_from_file('perl.c', qr/^\tfclose\(e_fp\);$/)) {
+ # need to patch perl.c to avoid calling fclose() twice on e_fp when
+ # using -e
+ # This diff is part of commit ab821d7fdc14a438. The second close was
+ # introduced with perl-5.002, commit a5f75d667838e8e7
+ # Might want a6c477ed8d4864e6 too, for the corresponding change to
+ # pp_ctl.c (likely without this, eval will have "fun")
+ apply_patch(<<'EOPATCH');
+diff --git a/perl.c b/perl.c
+index 03c4d48..3c814a2 100644
+--- a/perl.c
++++ b/perl.c
+@@ -252,6 +252,7 @@ setuid perl scripts securely.\n");
+ #ifndef VMS /* VMS doesn't have environ array */
+ origenviron = environ;
+ #endif
++ e_tmpname = Nullch;
+
+ if (do_undump) {
+
+@@ -405,6 +406,7 @@ setuid perl scripts securely.\n");
+ if (e_fp) {
+ if (Fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
+ croak("Can't write to temp file for -e: %s", Strerror(errno));
++ e_fp = Nullfp;
+ argc++,argv--;
+ scriptname = e_tmpname;
+ }
+@@ -470,10 +472,10 @@ setuid perl scripts securely.\n");
+ curcop->cop_line = 0;
+ curstash = defstash;
+ preprocess = FALSE;
+- if (e_fp) {
+- fclose(e_fp);
+- e_fp = Nullfp;
++ if (e_tmpname) {
+ (void)UNLINK(e_tmpname);
++ Safefree(e_tmpname);
++ e_tmpname = Nullch;
+ }
+
+ /* now that script is parsed, we can modify record separator */
+@@ -1369,7 +1371,7 @@ SV *sv;
+ scriptname = xfound;
+ }
+
+- origfilename = savepv(e_fp ? "-e" : scriptname);
++ origfilename = savepv(e_tmpname ? "-e" : scriptname);
+ curcop->cop_filegv = gv_fetchfile(origfilename);
+ if (strEQ(origfilename,"-"))
+ scriptname = "";
+
+EOPATCH
+ }
+
+ if ($major < 3 && $^O eq 'openbsd'
+ && !extract_from_file('pp_sys.c', qr/BSD_GETPGRP/)) {
+ # Part of commit c3293030fd1b7489
+ apply_patch(<<'EOPATCH');
+diff --git a/pp_sys.c b/pp_sys.c
+index 4608a2a..f0c9d1d 100644
+--- a/pp_sys.c
++++ b/pp_sys.c
+@@ -2903,8 +2903,8 @@ PP(pp_getpgrp)
+ pid = 0;
+ else
+ pid = SvIVx(POPs);
+-#ifdef USE_BSDPGRP
+- value = (I32)getpgrp(pid);
++#ifdef BSD_GETPGRP
++ value = (I32)BSD_GETPGRP(pid);
+ #else
+ if (pid != 0)
+ DIE("POSIX getpgrp can't take an argument");
+@@ -2933,8 +2933,8 @@ PP(pp_setpgrp)
+ }
+
+ TAINT_PROPER("setpgrp");
+-#ifdef USE_BSDPGRP
+- SETi( setpgrp(pid, pgrp) >= 0 );
++#ifdef BSD_SETPGRP
++ SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
+ #else
+ if ((pgrp != 0) || (pid != 0)) {
+ DIE("POSIX setpgrp can't take an argument");
+EOPATCH
+ }
+
+ if ($major < 4 && $^O eq 'openbsd') {
+ my $bad;
+ # Need changes from commit a6e633defa583ad5.
+ # Commits c07a80fdfe3926b5 and f82b3d4130164d5f changed the same part
+ # of perl.h
+
+ if (extract_from_file('perl.h',
+ qr/^#ifdef HAS_GETPGRP2$/)) {
+ $bad = <<'EOBAD';
+***************
+*** 57,71 ****
+ #define TAINT_PROPER(s) if (tainting) taint_proper(no_security, s)
+ #define TAINT_ENV() if (tainting) taint_env()
+
+! #ifdef HAS_GETPGRP2
+! # ifndef HAS_GETPGRP
+! # define HAS_GETPGRP
+! # endif
+! #endif
+!
+! #ifdef HAS_SETPGRP2
+! # ifndef HAS_SETPGRP
+! # define HAS_SETPGRP
+! # endif
+ #endif
+
+EOBAD
+ } elsif (extract_from_file('perl.h',
+ qr/Gack, you have one but not both of getpgrp2/)) {
+ $bad = <<'EOBAD';
+***************
+*** 56,76 ****
+ #define TAINT_PROPER(s) if (tainting) taint_proper(no_security, s)
+ #define TAINT_ENV() if (tainting) taint_env()
+
+! #if defined(HAS_GETPGRP2) && defined(HAS_SETPGRP2)
+! # define getpgrp getpgrp2
+! # define setpgrp setpgrp2
+! # ifndef HAS_GETPGRP
+! # define HAS_GETPGRP
+! # endif
+! # ifndef HAS_SETPGRP
+! # define HAS_SETPGRP
+! # endif
+! # ifndef USE_BSDPGRP
+! # define USE_BSDPGRP
+! # endif
+! #else
+! # if defined(HAS_GETPGRP2) || defined(HAS_SETPGRP2)
+! #include "Gack, you have one but not both of getpgrp2() and setpgrp2()."
+! # endif
+ #endif
+
+EOBAD
+ } elsif (extract_from_file('perl.h',
+ qr/^#ifdef USE_BSDPGRP$/)) {
+ $bad = <<'EOBAD'
+***************
+*** 91,116 ****
+ #define TAINT_PROPER(s) if (tainting) taint_proper(no_security, s)
+ #define TAINT_ENV() if (tainting) taint_env()
+
+! #ifdef USE_BSDPGRP
+! # ifdef HAS_GETPGRP
+! # define BSD_GETPGRP(pid) getpgrp((pid))
+! # endif
+! # ifdef HAS_SETPGRP
+! # define BSD_SETPGRP(pid, pgrp) setpgrp((pid), (pgrp))