+# </dev/null because it seems that some earlier versions of Configure can
+# call commands in a way that now has them reading from stdin (and hanging)
+my $pid = fork;
+die "Can't fork: $!" unless defined $pid;
+if (!$pid) {
+ open STDIN, '<', '/dev/null';
+ # If a file in MANIFEST is missing, Configure asks if you want to
+ # continue (the default being 'n'). With stdin closed or /dev/null,
+ # it exits immediately and the check for config.sh below will skip.
+ exec './Configure', @ARGS;
+ die "Failed to start Configure: $!";
+}
+waitpid $pid, 0
+ or die "wait for Configure, pid $pid failed: $!";
+
+patch_SH();
+
+if (-f 'config.sh') {
+ # Emulate noextensions if Configure doesn't support it.
+ fake_noextensions()
+ if $major < 10 && $defines{noextensions};
+ system './Configure -S </dev/null' and die;
+}
+
+if ($target =~ /config\.s?h/) {
+ match_and_exit($target) if $match && -f $target;
+ report_and_exit(!-f $target, 'could build', 'could not build', $target)
+ if $options{'test-build'};
+
+ skip("could not build $target") unless -f $target;
+
+ my $ret = system @ARGV;
+ report_and_exit($ret, 'zero exit from', 'non-zero exit from', "@ARGV");
+} elsif (!-f 'config.sh') {
+ # Skip if something went wrong with Configure
+
+ skip('could not build config.sh');
+}
+
+force_manifest_cleanup($missing, $created_dirs)
+ if $missing;
+
+if($options{'force-regen'}
+ && extract_from_file('Makefile', qr/\bregen_headers\b/)) {
+ # regen_headers was added in e50aee73b3d4c555, patch.1m for perl5.001
+ # It's not worth faking it for earlier revisions.
+ system "make regen_headers </dev/null"
+ and die;
+}
+
+patch_C();
+patch_ext();
+
+# Parallel build for miniperl is safe
+system "$options{make} $j miniperl </dev/null";
+
+# This is the file we expect make to create
+my $expected_file = $target =~ /^test/ ? 't/perl'
+ : $target eq 'Fcntl' ? "lib/auto/Fcntl/Fcntl.$Config{so}"
+ : $target;
+# This is the target we tell make to build in order to get $expected_file
+my $real_target = $target eq 'Fcntl' ? $expected_file : $target;
+
+if ($target ne 'miniperl') {
+ # Nearly all parallel build issues fixed by 5.10.0. Untrustworthy before that.
+ $j = '' if $major < 10;
+
+ if ($real_target eq 'test_prep') {
+ if ($major < 8) {
+ # test-prep was added in 5.004_01, 3e3baf6d63945cb6.
+ # renamed to test_prep in 2001 in 5fe84fd29acaf55c.
+ # earlier than that, just make test. It will be fast enough.
+ $real_target = extract_from_file('Makefile.SH',
+ qr/^(test[-_]prep):/,
+ 'test');
+ }
+ }
+
+ system "$options{make} $j $real_target </dev/null";
+}
+
+my $expected_file_found = $expected_file =~ /perl$/
+ ? -x $expected_file : -r $expected_file;
+
+if ($expected_file_found && $expected_file eq 't/perl') {
+ # Check that it isn't actually pointing to ../miniperl, which will happen
+ # if the sanity check ./miniperl -Ilib -MExporter -e '<?>' fails, and
+ # Makefile tries to run minitest.
+
+ # Of course, helpfully sometimes it's called ../perl, other times .././perl
+ # and who knows if that list is exhaustive...
+ my ($dev0, $ino0) = stat 't/perl';
+ my ($dev1, $ino1) = stat 'perl';
+ unless (defined $dev0 && defined $dev1 && $dev0 == $dev1 && $ino0 == $ino1) {
+ undef $expected_file_found;
+ my $link = readlink $expected_file;
+ warn "'t/perl' => '$link', not 'perl'";
+ die "Could not realink t/perl: $!" unless defined $link;
+ }
+}
+
+if ($options{'test-build'}) {
+ report_and_exit(!$expected_file_found, 'could build', 'could not build',
+ $real_target);
+} elsif (!$expected_file_found) {
+ skip("could not build $real_target");
+}
+
+match_and_exit($real_target) if $match;
+
+if (defined $options{'one-liner'}) {
+ my $exe = $target =~ /^(?:perl$|test)/ ? 'perl' : 'miniperl';
+ unshift @ARGV, '-e', $options{'one-liner'};
+ unshift @ARGV, '-l' if $options{l};
+ unshift @ARGV, '-w' if $options{w};
+ unshift @ARGV, "./$exe", '-Ilib';
+}
+
+# This is what we came here to run:
+
+if (exists $Config{ldlibpthname}) {
+ require Cwd;
+ my $varname = $Config{ldlibpthname};
+ my $cwd = Cwd::getcwd();
+ if (defined $ENV{$varname}) {
+ $ENV{$varname} = $cwd . $Config{path_sep} . $ENV{$varname};
+ } else {
+ $ENV{$varname} = $cwd;
+ }
+}
+
+my $ret = system @ARGV;
+
+report_and_exit($ret, 'zero exit from', 'non-zero exit from', "@ARGV");
+
+############################################################################
+#
+# Patching, editing and faking routines only below here.
+#
+############################################################################
+
+sub fake_noextensions {
+ edit_file('config.sh', sub {
+ my @lines = split /\n/, shift;
+ my @ext = split /\s+/, $defines{noextensions};
+ foreach (@lines) {
+ next unless /^extensions=/ || /^dynamic_ext/;
+ foreach my $ext (@ext) {
+ s/\b$ext( )?\b/$1/;
+ }
+ }
+ return join "\n", @lines;
+ });
+}
+
+sub force_manifest {
+ my (@missing, @created_dirs);
+ my $fh = open_or_die('MANIFEST');
+ while (<$fh>) {
+ next unless /^(\S+)/;
+ # -d is special case needed (at least) between 27332437a2ed1941 and
+ # bf3d9ec563d25054^ inclusive, as manifest contains ext/Thread/Thread
+ push @missing, $1
+ unless -f $1 || -d $1;
+ }
+ close_or_die($fh);
+
+ foreach my $pathname (@missing) {
+ my @parts = split '/', $pathname;
+ my $leaf = pop @parts;
+ my $path = '.';
+ while (@parts) {
+ $path .= '/' . shift @parts;
+ next if -d $path;
+ mkdir $path, 0700 or die "Can't create $path: $!";
+ unshift @created_dirs, $path;
+ }
+ $fh = open_or_die($pathname, '>');
+ close_or_die($fh);
+ chmod 0, $pathname or die "Can't chmod 0 $pathname: $!";
+ }
+ return \@missing, \@created_dirs;
+}
+
+sub force_manifest_cleanup {
+ my ($missing, $created_dirs) = @_;
+ # This is probably way too paranoid:
+ my @errors;
+ require Fcntl;
+ foreach my $file (@$missing) {
+ my (undef, undef, $mode, undef, undef, undef, undef, $size)
+ = stat $file;
+ if (!defined $mode) {
+ push @errors, "Added file $file has been deleted by Configure";
+ next;
+ }
+ if (Fcntl::S_IMODE($mode) != 0) {
+ push @errors,
+ sprintf 'Added file %s had mode changed by Configure to %03o',
+ $file, $mode;
+ }
+ if ($size != 0) {
+ push @errors,
+ "Added file $file had sized changed by Configure to $size";
+ }
+ unlink $file or die "Can't unlink $file: $!";
+ }
+ foreach my $dir (@$created_dirs) {
+ rmdir $dir or die "Can't rmdir $dir: $!";
+ }
+ skip("@errors")
+ if @errors;
+}
+
+sub patch_Configure {
+ if ($major < 1) {
+ if (extract_from_file('Configure',
+ qr/^\t\t\*=\*\) echo "\$1" >> \$optdef;;$/)) {
+ # This is " Spaces now allowed in -D command line options.",
+ # part of commit ecfc54246c2a6f42
+ apply_patch(<<'EOPATCH');
+diff --git a/Configure b/Configure
+index 3d3b38d..78ffe16 100755
+--- a/Configure
++++ b/Configure
+@@ -652,7 +777,8 @@ while test $# -gt 0; do
+ echo "$me: use '-U symbol=', not '-D symbol='." >&2
+ echo "$me: ignoring -D $1" >&2
+ ;;
+- *=*) echo "$1" >> $optdef;;
++ *=*) echo "$1" | \
++ sed -e "s/'/'\"'\"'/g" -e "s/=\(.*\)/='\1'/" >> $optdef;;
+ *) echo "$1='define'" >> $optdef;;
+ esac
+ shift
+EOPATCH
+ }
+
+ if (extract_from_file('Configure', qr/^if \$contains 'd_namlen' \$xinc\b/)) {
+ # Configure's original simple "grep" for d_namlen falls foul of the
+ # approach taken by the glibc headers:
+ # #ifdef _DIRENT_HAVE_D_NAMLEN
+ # # define _D_EXACT_NAMLEN(d) ((d)->d_namlen)
+ #
+ # where _DIRENT_HAVE_D_NAMLEN is not defined on Linux.
+ # This is also part of commit ecfc54246c2a6f42
+ apply_patch(<<'EOPATCH');
+diff --git a/Configure b/Configure
+index 3d3b38d..78ffe16 100755
+--- a/Configure
++++ b/Configure
+@@ -3935,7 +4045,8 @@ $rm -f try.c
+
+ : see if the directory entry stores field length
+ echo " "
+-if $contains 'd_namlen' $xinc >/dev/null 2>&1; then
++$cppstdin $cppflags $cppminus < "$xinc" > try.c
++if $contains 'd_namlen' try.c >/dev/null 2>&1; then
+ echo "Good, your directory entry keeps length information in d_namlen." >&4
+ val="$define"
+ else
+EOPATCH
+ }
+ }
+
+ if ($major < 2
+ && !extract_from_file('Configure',
+ qr/Try to guess additional flags to pick up local libraries/)) {
+ my $mips = extract_from_file('Configure',
+ qr!(''\) if (?:\./)?mips; then)!);
+ # This is part of perl-5.001n. It's needed, to add -L/usr/local/lib to
+ # theld flags if libraries are found there. It shifts the code to set up
+ # libpth earlier, and then adds the code to add libpth entries to
+ # ldflags
+ # mips was changed to ./mips in ecfc54246c2a6f42, perl5.000 patch.0g
+ apply_patch(sprintf <<'EOPATCH', $mips);
+diff --git a/Configure b/Configure
+index 53649d5..0635a6e 100755
+--- a/Configure
++++ b/Configure
+@@ -2749,6 +2749,52 @@ EOM
+ ;;
+ esac
+
++: Set private lib path
++case "$plibpth" in
++'') if ./mips; then
++ plibpth="$incpath/usr/lib /usr/local/lib /usr/ccs/lib"
++ fi;;
++esac
++case "$libpth" in
++' ') dlist='';;
++'') dlist="$plibpth $glibpth";;
++*) dlist="$libpth";;
++esac
++
++: Now check and see which directories actually exist, avoiding duplicates
++libpth=''
++for xxx in $dlist
++do
++ if $test -d $xxx; then
++ case " $libpth " in
++ *" $xxx "*) ;;
++ *) libpth="$libpth $xxx";;
++ esac
++ fi
++done
++$cat <<'EOM'
++
++Some systems have incompatible or broken versions of libraries. Among
++the directories listed in the question below, please remove any you
++know not to be holding relevant libraries, and add any that are needed.
++Say "none" for none.
++
++EOM
++case "$libpth" in
++'') dflt='none';;
++*)
++ set X $libpth
++ shift
++ dflt=${1+"$@"}
++ ;;
++esac
++rp="Directories to use for library searches?"
++. ./myread
++case "$ans" in
++none) libpth=' ';;
++*) libpth="$ans";;
++esac
++
+ : flags used in final linking phase
+ case "$ldflags" in
+ '') if ./venix; then
+@@ -2765,6 +2811,23 @@ case "$ldflags" in
+ ;;
+ *) dflt="$ldflags";;
+ esac
++
++: Possible local library directories to search.
++loclibpth="/usr/local/lib /opt/local/lib /usr/gnu/lib"
++loclibpth="$loclibpth /opt/gnu/lib /usr/GNU/lib /opt/GNU/lib"
++
++: Try to guess additional flags to pick up local libraries.
++for thislibdir in $libpth; do
++ case " $loclibpth " in
++ *" $thislibdir "*)
++ case "$dflt " in
++ "-L$thislibdir ") ;;
++ *) dflt="$dflt -L$thislibdir" ;;
++ esac
++ ;;
++ esac
++done
++
+ echo " "
+ rp="Any additional ld flags (NOT including libraries)?"
+ . ./myread
+@@ -2828,52 +2891,6 @@ n) echo "OK, that should do.";;
+ esac
+ $rm -f try try.* core
+
+-: Set private lib path
+-case "$plibpth" in
+-%s
+- plibpth="$incpath/usr/lib /usr/local/lib /usr/ccs/lib"
+- fi;;
+-esac
+-case "$libpth" in
+-' ') dlist='';;
+-'') dlist="$plibpth $glibpth";;
+-*) dlist="$libpth";;
+-esac
+-
+-: Now check and see which directories actually exist, avoiding duplicates
+-libpth=''
+-for xxx in $dlist
+-do
+- if $test -d $xxx; then
+- case " $libpth " in
+- *" $xxx "*) ;;
+- *) libpth="$libpth $xxx";;
+- esac
+- fi
+-done
+-$cat <<'EOM'
+-
+-Some systems have incompatible or broken versions of libraries. Among
+-the directories listed in the question below, please remove any you
+-know not to be holding relevant libraries, and add any that are needed.
+-Say "none" for none.
+-
+-EOM
+-case "$libpth" in
+-'') dflt='none';;
+-*)
+- set X $libpth
+- shift
+- dflt=${1+"$@"}
+- ;;
+-esac
+-rp="Directories to use for library searches?"
+-. ./myread
+-case "$ans" in
+-none) libpth=' ';;
+-*) libpth="$ans";;
+-esac
+-
+ : compute shared library extension
+ case "$so" in
+ '')
+EOPATCH
+ }
+
+ if ($major < 5 && extract_from_file('Configure',
+ qr!if \$cc \$ccflags try\.c -o try >/dev/null 2>&1; then!)) {
+ # Analogous to the more general fix of dfe9444ca7881e71
+ # Without this flags such as -m64 may not be passed to this compile,
+ # which results in a byteorder of '1234' instead of '12345678', which
+ # can then cause crashes.
+
+ if (extract_from_file('Configure', qr/xxx_prompt=y/)) {
+ # 8e07c86ebc651fe9 or later
+ # ("This is my patch patch.1n for perl5.001.")
+ apply_patch(<<'EOPATCH');
+diff --git a/Configure b/Configure
+index 62249dd..c5c384e 100755
+--- a/Configure
++++ b/Configure
+@@ -8247,7 +8247,7 @@ main()
+ }
+ EOCP
+ xxx_prompt=y
+- if $cc $ccflags try.c -o try >/dev/null 2>&1 && ./try > /dev/null; then
++ if $cc $ccflags $ldflags try.c -o try >/dev/null 2>&1 && ./try > /dev/null; then
+ dflt=`./try`
+ case "$dflt" in
+ [1-4][1-4][1-4][1-4]|12345678|87654321)
+EOPATCH
+ } else {
+ apply_patch(<<'EOPATCH');
+diff --git a/Configure b/Configure
+index 53649d5..f1cd64a 100755
+--- a/Configure
++++ b/Configure
+@@ -6362,7 +6362,7 @@ main()
+ printf("\n");
+ }
+ EOCP
+- if $cc $ccflags try.c -o try >/dev/null 2>&1 ; then
++ if $cc $ccflags $ldflags try.c -o try >/dev/null 2>&1 ; then
+ dflt=`./try`
+ case "$dflt" in
+ ????|????????) echo "(The test program ran ok.)";;
+EOPATCH
+ }
+ }
+
+ if ($major < 6 && !extract_from_file('Configure',
+ qr!^\t-A\)$!)) {
+ # This adds the -A option to Configure, which is incredibly useful
+ # Effectively this is commits 02e93a22d20fc9a5, 5f83a3e9d818c3ad,
+ # bde6b06b2c493fef, f7c3111703e46e0c and 2 lines of trailing whitespace
+ # removed by 613d6c3e99b9decc, but applied at slightly different
+ # locations to ensure a clean patch back to 5.000
+ # Note, if considering patching to the intermediate revisions to fix
+ # bugs in -A handling, f7c3111703e46e0c is from 2002, and hence
+ # $major == 8
+
+ # To add to the fun, early patches add -K and -O options, and it's not
+ # trivial to get patch to put the C<. ./posthint.sh> in the right place
+ edit_file('Configure', sub {
+ my $code = shift;
+ $code =~ s/(optstr = ")([^"]+";\s*# getopt-style specification)/$1A:$2/
+ or die "Substitution failed";
+ $code =~ s!^(: who configured the system)!
+touch posthint.sh
+. ./posthint.sh
+
+$1!ms
+ or die "Substitution failed";
+ return $code;
+ });
+ apply_patch(<<'EOPATCH');
+diff --git a/Configure b/Configure
+index 4b55fa6..60c3c64 100755
+--- a/Configure
++++ b/Configure
+@@ -1150,6 +1150,7 @@ set X `for arg in "$@"; do echo "X$arg"; done |
+ eval "set $*"
+ shift
+ rm -f options.awk
++rm -f posthint.sh
+
+ : set up default values
+ fastread=''
+@@ -1172,6 +1173,56 @@ while test $# -gt 0; do
+ case "$1" in
+ -d) shift; fastread=yes;;
+ -e) shift; alldone=cont;;
++ -A)
++ shift
++ xxx=''
++ yyy="$1"
++ zzz=''
++ uuu=undef
++ case "$yyy" in
++ *=*) zzz=`echo "$yyy"|sed 's!=.*!!'`
++ case "$zzz" in
++ *:*) zzz='' ;;
++ *) xxx=append
++ zzz=" "`echo "$yyy"|sed 's!^[^=]*=!!'`
++ yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
++ esac
++ ;;
++ esac
++ case "$xxx" in
++ '') case "$yyy" in
++ *:*) xxx=`echo "$yyy"|sed 's!:.*!!'`
++ yyy=`echo "$yyy"|sed 's!^[^:]*:!!'`
++ zzz=`echo "$yyy"|sed 's!^[^=]*=!!'`
++ yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
++ *) xxx=`echo "$yyy"|sed 's!:.*!!'`
++ yyy=`echo "$yyy"|sed 's!^[^:]*:!!'` ;;
++ esac
++ ;;
++ esac
++ case "$xxx" in
++ append)
++ echo "$yyy=\"\${$yyy}$zzz\"" >> posthint.sh ;;
++ clear)
++ echo "$yyy=''" >> posthint.sh ;;
++ define)
++ case "$zzz" in
++ '') zzz=define ;;
++ esac
++ echo "$yyy='$zzz'" >> posthint.sh ;;
++ eval)
++ echo "eval \"$yyy=$zzz\"" >> posthint.sh ;;
++ prepend)
++ echo "$yyy=\"$zzz\${$yyy}\"" >> posthint.sh ;;
++ undef)
++ case "$zzz" in
++ '') zzz="$uuu" ;;
++ esac
++ echo "$yyy=$zzz" >> posthint.sh ;;
++ *) echo "$me: unknown -A command '$xxx', ignoring -A $1" >&2 ;;
++ esac
++ shift
++ ;;
+ -f)
+ shift
+ cd ..
+EOPATCH
+ }
+
+ if ($major < 8 && $^O eq 'aix') {
+ edit_file('Configure', sub {
+ my $code = shift;
+ # Replicate commit a8c676c69574838b
+ # Whitespace allowed at the ends of /lib/syscalls.exp lines
+ # and half of commit c6912327ae30e6de
+ # AIX syscalls.exp scan: the syscall might be marked 32, 3264, or 64
+ $code =~ s{(\bsed\b.*\bsyscall)(?:\[0-9\]\*)?(\$.*/lib/syscalls\.exp)}
+ {$1 . "[0-9]*[ \t]*" . $2}e;
+ return $code;
+ });
+ }
+
+ if ($major < 8 && !extract_from_file('Configure',
+ qr/^\t\tif test ! -t 0; then$/)) {
+ # Before dfe9444ca7881e71, Configure would refuse to run if stdin was
+ # not a tty. With that commit, the tty requirement was dropped for -de
+ # and -dE
+ # Commit aaeb8e512e8e9e14 dropped the tty requirement for -S
+ # For those older versions, it's probably easiest if we simply remove
+ # the sanity test.
+ edit_file('Configure', sub {
+ my $code = shift;
+ $code =~ s/test ! -t 0/test Perl = rules/;
+ return $code;
+ });
+ }
+
+ if ($major == 8 || $major == 9) {
+ # Fix symbol detection to that of commit 373dfab3839ca168 if it's any
+ # intermediate version 5129fff43c4fe08c or later, as the intermediate
+ # versions don't work correctly on (at least) Sparc Linux.
+ # 5129fff43c4fe08c adds the first mention of mistrustnm.
+ # 373dfab3839ca168 removes the last mention of lc=""
+ edit_file('Configure', sub {
+ my $code = shift;
+ return $code
+ if $code !~ /\btc="";/; # 373dfab3839ca168 or later
+ return $code
+ if $code !~ /\bmistrustnm\b/; # before 5129fff43c4fe08c
+ my $fixed = <<'EOC';
+
+: is a C symbol defined?
+csym='tlook=$1;
+case "$3" in
+-v) tf=libc.tmp; tdc="";;
+-a) tf=libc.tmp; tdc="[]";;
+*) tlook="^$1\$"; tf=libc.list; tdc="()";;
+esac;
+tx=yes;
+case "$reuseval-$4" in
+true-) ;;
+true-*) tx=no; eval "tval=\$$4"; case "$tval" in "") tx=yes;; esac;;
+esac;
+case "$tx" in
+yes)
+ tval=false;
+ if $test "$runnm" = true; then
+ if $contains $tlook $tf >/dev/null 2>&1; then
+ tval=true;
+ elif $test "$mistrustnm" = compile -o "$mistrustnm" = run; then
+ echo "void *(*(p()))$tdc { extern void *$1$tdc; return &$1; } int main() { if(p()) return(0); else return(1); }"> try.c;
+ $cc -o try $optimize $ccflags $ldflags try.c >/dev/null 2>&1 $libs && tval=true;
+ $test "$mistrustnm" = run -a -x try && { $run ./try$_exe >/dev/null 2>&1 || tval=false; };
+ $rm -f try$_exe try.c core core.* try.core;
+ fi;
+ else
+ echo "void *(*(p()))$tdc { extern void *$1$tdc; return &$1; } int main() { if(p()) return(0); else return(1); }"> try.c;
+ $cc -o try $optimize $ccflags $ldflags try.c $libs >/dev/null 2>&1 && tval=true;
+ $rm -f try$_exe try.c;
+ fi;
+ ;;
+*)
+ case "$tval" in
+ $define) tval=true;;
+ *) tval=false;;
+ esac;
+ ;;
+esac;
+eval "$2=$tval"'
+
+EOC
+ $code =~ s/\n: is a C symbol defined\?\n.*?\neval "\$2=\$tval"'\n\n/$fixed/sm
+ or die "substitution failed";
+ return $code;
+ });
+ }
+
+ if ($major < 10
+ && extract_from_file('Configure', qr/^set malloc\.h i_malloc$/)) {
+ # This is commit 01d07975f7ef0e7d, trimmed, with $compile inlined as
+ # prior to bd9b35c97ad661cc Configure had the malloc.h test before the
+ # definition of $compile.