This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In bisect-runner.pl, refactor the code that applies and reverts commits.
[perl5.git] / Porting / bisect-runner.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 use Getopt::Long qw(:config bundling no_auto_abbrev);
5 use Pod::Usage;
6 use Config;
7 use Carp;
8
9 my @targets
10     = qw(config.sh config.h miniperl lib/Config.pm Fcntl perl test_prep);
11
12 my $cpus;
13 if (open my $fh, '<', '/proc/cpuinfo') {
14     while (<$fh>) {
15         ++$cpus if /^processor\s+:\s+\d+$/;
16     }
17 } elsif (-x '/sbin/sysctl') {
18     $cpus = 1 + $1 if `/sbin/sysctl hw.ncpu` =~ /^hw\.ncpu: (\d+)$/;
19 } elsif (-x '/usr/bin/getconf') {
20     $cpus = 1 + $1 if `/usr/bin/getconf _NPROCESSORS_ONLN` =~ /^(\d+)$/;
21 }
22
23 my %options =
24     (
25      jobs => defined $cpus ? $cpus + 1 : 2,
26      'expect-pass' => 1,
27      clean => 1, # mostly for debugging this
28     );
29
30 my $linux64 = `uname -sm` eq "Linux x86_64\n" ? '64' : '';
31
32 my @paths;
33
34 if ($^O eq 'linux') {
35     # This is the search logic for a multi-arch library layout
36     # added to linux.sh in commits 40f026236b9959b7 and dcffd848632af2c7.
37     my $gcc = -x '/usr/bin/gcc' ? '/usr/bin/gcc' : 'gcc';
38
39     foreach (`$gcc -print-search-dirs`) {
40         next unless /^libraries: =(.*)/;
41         foreach (split ':', $1) {
42             next if m/gcc/;
43             next unless -d $_;
44             s!/$!!;
45             push @paths, $_;
46         }
47     }
48 }
49
50 push @paths, map {$_ . $linux64} qw(/usr/local/lib /lib /usr/lib);
51
52 my %defines =
53     (
54      usedevel => '',
55      optimize => '-g',
56      cc => 'ccache cc',
57      ld => 'cc',
58      ($linux64 ? (libpth => \@paths) : ()),
59     );
60
61 unless(GetOptions(\%options,
62                   'target=s', 'jobs|j=i', 'expect-pass=i',
63                   'expect-fail' => sub { $options{'expect-pass'} = 0; },
64                   'clean!', 'one-liner|e=s', 'match=s', 'force-manifest',
65                   'test-build', 'check-args', 'A=s@', 'usage|help|?',
66                   'D=s@' => sub {
67                       my (undef, $val) = @_;
68                       if ($val =~ /\A([^=]+)=(.*)/s) {
69                           $defines{$1} = length $2 ? $2 : "\0";
70                       } else {
71                           $defines{$val} = '';
72                       }
73                   },
74                   'U=s@' => sub {
75                       $defines{$_[1]} = undef;
76                   },
77                  )) {
78     pod2usage(exitval => 255, verbose => 1);
79 }
80
81 my ($target, $j, $match) = @options{qw(target jobs match)};
82
83 pod2usage(exitval => 255, verbose => 1) if $options{usage};
84 pod2usage(exitval => 255, verbose => 1)
85     unless @ARGV || $match || $options{'test-build'} || defined $options{'one-liner'};
86
87 exit 0 if $options{'check-args'};
88
89 =head1 NAME
90
91 bisect.pl - use git bisect to pinpoint changes
92
93 =head1 SYNOPSIS
94
95     # When did this become an error?
96     .../Porting/bisect.pl -e 'my $a := 2;'
97     # When did this stop being an error?
98     .../Porting/bisect.pl --expect-fail -e '1 // 2'
99     # When did this stop matching?
100     .../Porting/bisect.pl --match '\b(?:PL_)hash_seed_set\b'
101     # When did this start matching?
102     .../Porting/bisect.pl --expect-fail --match '\buseithreads\b'
103     # When did this test program stop working?
104     .../Porting/bisect.pl -- ./perl -Ilib ../test_prog.pl
105     # When did this first become valid syntax?
106     .../Porting/bisect.pl --target=miniperl --end=v5.10.0 \
107          --expect-fail -e 'my $a := 2;'
108     # What was the last revision to build with these options?
109     .../Porting/bisect.pl --test-build -Dd_dosuid
110
111 =head1 DESCRIPTION
112
113 Together F<bisect.pl> and F<bisect-runner.pl> attempt to automate the use
114 of C<git bisect> as much as possible. With one command (and no other files)
115 it's easy to find out
116
117 =over 4
118
119 =item *
120
121 Which commit caused this example code to break?
122
123 =item *
124
125 Which commit caused this example code to start working?
126
127 =item *
128
129 Which commit added the first to match this regex?
130
131 =item *
132
133 Which commit removed the last to match this regex?
134
135 =back
136
137 usually without needing to know which versions of perl to use as start and
138 end revisions.
139
140 By default F<bisect.pl> will process all options, then use the rest of the
141 command line as arguments to list C<system> to run a test case. By default,
142 the test case should pass (exit with 0) on earlier perls, and fail (exit
143 non-zero) on I<blead>. F<bisect.pl> will use F<bisect-runner.pl> to find the
144 earliest stable perl version on which the test case passes, check that it
145 fails on blead, and then use F<bisect-runner.pl> with C<git bisect run> to
146 find the commit which caused the failure.
147
148 Because the test case is the complete argument to C<system>, it is easy to
149 run something other than the F<perl> built, if necessary. If you need to run
150 the perl built, you'll probably need to invoke it as C<./perl -Ilib ...>
151
152 You need a clean checkout to run a bisect, and you can't use the checkout
153 which contains F<Porting/bisect.pl> (because C<git bisect>) will check out
154 a revision before F<Porting/bisect-runner.pl> was added, which
155 C<git bisect run> needs). If your working checkout is called F<perl>, the
156 simplest solution is to make a local clone, and run from that. I<i.e.>:
157
158     cd ..
159     git clone perl perl2
160     cd perl2
161     ../perl/Porting/bisect.pl ...
162
163 By default, F<bisect-runner.pl> will automatically disable the build of
164 L<DB_File> for commits earlier than ccb44e3bf3be2c30, as it's not practical
165 to patch DB_File 1.70 and earlier to build with current Berkeley DB headers.
166 (ccb44e3bf3be2c30 was in September 1999, between 5.005_62 and 5.005_63.)
167 If your F<db.h> is old enough you can override this with C<-Unoextensions>.
168
169 =head1 OPTIONS
170
171 =over 4
172
173 =item *
174
175 --start I<commit-ish>
176
177 Earliest revision to test, as a I<commit-ish> (a tag, commit or anything
178 else C<git> understands as a revision). If not specified, F<bisect.pl> will
179 search stable perl releases from 5.002 to 5.14.0 until it finds one where
180 the test case passes.
181
182 =item *
183
184 --end I<commit-ish>
185
186 Most recent revision to test, as a I<commit-ish>. If not specified, defaults
187 to I<blead>.
188
189 =item *
190
191 --target I<target>
192
193 F<Makefile> target (or equivalent) needed, to run the test case. If specified,
194 this should be one of
195
196 =over 4
197
198 =item *
199
200 I<config.sh>
201
202 Just run F<./Configure>
203
204 =item *
205
206 I<config.h>
207
208 Run the various F<*.SH> files to generate F<Makefile>, F<config.h>, I<etc>.
209
210 =item *
211
212 I<miniperl>
213
214 Build F<miniperl>.
215
216 =item *
217
218 I<lib/Config.pm>
219
220 Use F<miniperl> to build F<lib/Config.pm>
221
222 =item *
223
224 I<Fcntl>
225
226 Build F<lib/auto/Fcntl/Fnctl.so> (strictly, C<.$Config{so}>). As L<Fcntl>
227 is simple XS module present since 5.000, this provides a fast test of
228 whether XS modules can be built. Note, XS modules are built by F<miniperl>,
229 hence this target will not build F<perl>.
230
231 =item *
232
233 I<perl>
234
235 Build F<perl>. This also builds pure-Perl modules in F<cpan>, F<dist> and
236 F<ext>. XS modules (such as L<Fcntl>) are not built.
237
238 =item *
239
240 I<test_prep>
241
242 Build everything needed to run the tests. This is the default if we're
243 running test code, but is time consuming, as it means building all
244 XS modules. For older F<Makefile>s, the previous name of C<test-prep>
245 is automatically substituted. For very old F<Makefile>s, C<make test> is
246 run, as there is no target provided to just get things ready, and for 5.004
247 and earlier the tests run very quickly.
248
249 =back
250
251 =item *
252
253 --one-liner 'code to run'
254
255 =item *
256
257 -e 'code to run'
258
259 Example code to run, just like you'd use with C<perl -e>.
260
261 This prepends C<./perl -Ilib -e 'code to run'> to the test case given,
262 or F<./miniperl> if I<target> is C<miniperl>.
263
264 (Usually you'll use C<-e> instead of providing a test case in the
265 non-option arguments to F<bisect.pl>)
266
267 C<-E> intentionally isn't supported, as it's an error in 5.8.0 and earlier,
268 which interferes with detecting errors in the example code itself.
269
270 =item *
271
272 --expect-fail
273
274 The test case should fail for the I<start> revision, and pass for the I<end>
275 revision. The bisect run will find the first commit where it passes.
276
277 =item *
278
279 -Dnoextensions=Encode
280
281 =item *
282
283 -Uusedevel
284
285 =item *
286
287 -Accflags=-DNO_MATHOMS
288
289 Arguments to pass to F<Configure>. Repeated C<-A> arguments are passed
290 through as is. C<-D> and C<-U> are processed in order, and override
291 previous settings for the same parameter. F<bisect-runner.pl> emulates
292 C<-Dnoextensions> when F<Configure> itself does not provide it, as it's
293 often very useful to be able to disable some XS extensions.
294
295 =item *
296
297 --jobs I<jobs>
298
299 =item *
300
301 -j I<jobs>
302
303 Number of C<make> jobs to run in parallel. If F</proc/cpuinfo> exists and
304 can be parsed, or F</sbin/sysctl> exists and reports C<hw.ncpu>, or
305 F</usr/bin/getconf> exists and reports C<_NPROCESSORS_ONLN> defaults to 1 +
306 I<number of CPUs>. Otherwise defaults to 2.
307
308 =item *
309
310 --match pattern
311
312 Instead of running a test program to determine I<pass> or I<fail>, pass
313 if the given regex matches, and hence search for the commit that removes
314 the last matching file.
315
316 If no I<target> is specified, the match is against all files in the
317 repository (which is fast). If a I<target> is specified, that target is
318 built, and the match is against only the built files. C<--expect-fail> can
319 be used with C<--match> to search for a commit that adds files that match.
320
321 =item *
322
323 --test-build
324
325 Test that the build completes, without running any test case.
326
327 By default, if the build for the desired I<target> fails to complete,
328 F<bisect-runner.pl> reports a I<skip> back to C<git bisect>, the assumption
329 being that one wants to find a commit which changed state "builds && passes"
330 to "builds && fails". If instead one is interested in which commit broke the
331 build (possibly for particular F<Configure> options), use I<--test-build>
332 to treat a build failure as a failure, not a "skip".
333
334 Often this option isn't as useful as it first seems, because I<any> build
335 failure will be reported to C<git bisect> as a failure, not just the failure
336 that you're interested in. Generally, to debug a particular problem, it's
337 more useful to use a I<target> that builds properly at the point of interest,
338 and then a test case that runs C<make>. For example:
339
340     .../Porting/bisect.pl --start=perl-5.000 --end=perl-5.002 \
341         --expect-fail --force-manifest --target=miniperl make perl
342
343 will find the first revision capable of building L<DynaLoader> and then
344 F<perl>, without becoming confused by revisions where F<miniperl> won't
345 even link.
346
347 =item *
348
349 --force-manifest
350
351 By default, a build will "skip" if any files listed in F<MANIFEST> are not
352 present. Usually this is useful, as it avoids false-failures. However, there
353 are some long ranges of commits where listed files are missing, which can
354 cause a bisect to abort because all that remain are skipped revisions.
355
356 In these cases, particularly if the test case uses F<miniperl> and no modules,
357 it may be more useful to force the build to continue, even if files
358 F<MANIFEST> are missing.
359
360 =item *
361
362 --expect-pass [0|1]
363
364 C<--expect-pass=0> is equivalent to C<--expect-fail>. I<1> is the default.
365
366 =item *
367
368 --no-clean
369
370 Tell F<bisect-runner.pl> not to clean up after the build. This allows one
371 to use F<bisect-runner.pl> to build the current particular perl revision for
372 interactive testing, or for debugging F<bisect-runner.pl>.
373
374 Passing this to F<bisect.pl> will likely cause the bisect to fail badly.
375
376 =item *
377
378 --check-args
379
380 Validate the options and arguments, and exit silently if they are valid.
381
382 =item *
383
384 --usage
385
386 =item *
387
388 --help
389
390 =item *
391
392 -?
393
394 Display the usage information and exit.
395
396 =back
397
398 =cut
399
400 die "$0: Can't build $target" if defined $target && !grep {@targets} $target;
401
402 $j = "-j$j" if $j =~ /\A\d+\z/;
403
404 # Sadly, however hard we try, I don't think that it will be possible to build
405 # modules in ext/ on x86_64 Linux before commit e1666bf5602ae794 on 1999/12/29,
406 # which updated to MakeMaker 3.7, which changed from using a hard coded ld
407 # in the Makefile to $(LD). On x86_64 Linux the "linker" is gcc.
408
409 sub open_or_die {
410     my $file = shift;
411     my $mode = @_ ? shift : '<';
412     open my $fh, $mode, $file or croak("Can't open $file: $!");
413     ${*$fh{SCALAR}} = $file;
414     return $fh;
415 }
416
417 sub close_or_die {
418     my $fh = shift;
419     return if close $fh;
420     croak("Can't close: $!") unless ref $fh eq 'GLOB';
421     croak("Can't close ${*$fh{SCALAR}}: $!");
422 }
423
424 sub extract_from_file {
425     my ($file, $rx, $default) = @_;
426     my $fh = open_or_die($file);
427     while (<$fh>) {
428         my @got = $_ =~ $rx;
429         return wantarray ? @got : $got[0]
430             if @got;
431     }
432     return $default if defined $default;
433     return;
434 }
435
436 sub edit_file {
437     my ($file, $munger) = @_;
438     local $/;
439     my $fh = open_or_die($file);
440     my $orig = <$fh>;
441     die "Can't read $file: $!" unless defined $orig && close $fh;
442     my $new = $munger->($orig);
443     return if $new eq $orig;
444     $fh = open_or_die($file, '>');
445     print $fh $new or die "Can't print to $file: $!";
446     close_or_die($fh);
447 }
448
449 sub apply_patch {
450     my $patch = shift;
451
452     my ($file) = $patch =~ qr!^diff.*a/(\S+) b/\1!;
453     open my $fh, '|-', 'patch', '-p1' or die "Can't run patch: $!";
454     print $fh $patch;
455     return if close $fh;
456     print STDERR "Patch is <<'EOPATCH'\n${patch}EOPATCH\n";
457     die "Can't patch $file: $?, $!";
458 }
459
460 sub apply_commit {
461     my ($commit, @files) = @_;
462     return unless system "git show $commit @files | patch -p1";
463     die "Can't apply commit $commit to @files" if @files;
464     die "Can't apply commit $commit";
465 }
466
467 sub revert_commit {
468     my ($commit, @files) = @_;
469     return unless system "git show -R $commit @files | patch -p1";
470     die "Can't apply revert $commit from @files" if @files;
471     die "Can't apply revert $commit";
472 }
473
474 sub clean {
475     if ($options{clean}) {
476         # Needed, because files that are build products in this checked out
477         # version might be in git in the next desired version.
478         system 'git clean -dxf </dev/null';
479         # Needed, because at some revisions the build alters checked out files.
480         # (eg pod/perlapi.pod). Also undoes any changes to makedepend.SH
481         system 'git reset --hard HEAD </dev/null';
482     }
483 }
484
485 sub skip {
486     my $reason = shift;
487     clean();
488     warn "skipping - $reason";
489     exit 125;
490 }
491
492 sub report_and_exit {
493     my ($ret, $pass, $fail, $desc) = @_;
494
495     clean();
496
497     my $got = ($options{'expect-pass'} ? !$ret : $ret) ? 'good' : 'bad';
498     if ($ret) {
499         print "$got - $fail $desc\n";
500     } else {
501         print "$got - $pass $desc\n";
502     }
503
504     exit($got eq 'bad');
505 }
506
507 sub match_and_exit {
508     my $target = shift;
509     my $matches = 0;
510     my $re = qr/$match/;
511     my @files;
512
513     {
514         local $/ = "\0";
515         @files = defined $target ? `git ls-files -o -z`: `git ls-files -z`;
516         chomp @files;
517     }
518
519     foreach my $file (@files) {
520         my $fh = open_or_die($file);
521         while (<$fh>) {
522             if ($_ =~ $re) {
523                 ++$matches;
524                 if (tr/\t\r\n -~\200-\377//c) {
525                     print "Binary file $file matches\n";
526                 } else {
527                     $_ .= "\n" unless /\n\z/;
528                     print "$file: $_";
529                 }
530             }
531         }
532         close_or_die($fh);
533     }
534     report_and_exit(!$matches,
535                     $matches == 1 ? '1 match for' : "$matches matches for",
536                     'no matches for', $match);
537 }
538
539 # Not going to assume that system perl is yet new enough to have autodie
540 system 'git clean -dxf </dev/null' and die;
541
542 if (!defined $target) {
543     match_and_exit() if $match;
544     $target = 'test_prep';
545 }
546
547 skip('no Configure - is this the //depot/perlext/Compiler branch?')
548     unless -f 'Configure';
549
550 # This changes to PERL_VERSION in 4d8076ea25903dcb in 1999
551 my $major
552     = extract_from_file('patchlevel.h',
553                         qr/^#define\s+(?:PERL_VERSION|PATCHLEVEL)\s+(\d+)\s/,
554                         0);
555
556 if ($major < 1) {
557     if (extract_from_file('Configure',
558                           qr/^          \*=\*\) echo "\$1" >> \$optdef;;$/)) {
559         # This is "        Spaces now allowed in -D command line options.",
560         # part of commit ecfc54246c2a6f42
561         apply_patch(<<'EOPATCH');
562 diff --git a/Configure b/Configure
563 index 3d3b38d..78ffe16 100755
564 --- a/Configure
565 +++ b/Configure
566 @@ -652,7 +777,8 @@ while test $# -gt 0; do
567                         echo "$me: use '-U symbol=', not '-D symbol='." >&2
568                         echo "$me: ignoring -D $1" >&2
569                         ;;
570 -               *=*) echo "$1" >> $optdef;;
571 +               *=*) echo "$1" | \
572 +                               sed -e "s/'/'\"'\"'/g" -e "s/=\(.*\)/='\1'/" >> $optdef;;
573                 *) echo "$1='define'" >> $optdef;;
574                 esac
575                 shift
576 EOPATCH
577     }
578     if (extract_from_file('Configure', qr/^if \$contains 'd_namlen' \$xinc\b/)) {
579         # Configure's original simple "grep" for d_namlen falls foul of the
580         # approach taken by the glibc headers:
581         # #ifdef _DIRENT_HAVE_D_NAMLEN
582         # # define _D_EXACT_NAMLEN(d) ((d)->d_namlen)
583         #
584         # where _DIRENT_HAVE_D_NAMLEN is not defined on Linux.
585         # This is also part of commit ecfc54246c2a6f42
586         apply_patch(<<'EOPATCH');
587 diff --git a/Configure b/Configure
588 index 3d3b38d..78ffe16 100755
589 --- a/Configure
590 +++ b/Configure
591 @@ -3935,7 +4045,8 @@ $rm -f try.c
592  
593  : see if the directory entry stores field length
594  echo " "
595 -if $contains 'd_namlen' $xinc >/dev/null 2>&1; then
596 +$cppstdin $cppflags $cppminus < "$xinc" > try.c
597 +if $contains 'd_namlen' try.c >/dev/null 2>&1; then
598         echo "Good, your directory entry keeps length information in d_namlen." >&4
599         val="$define"
600  else
601 EOPATCH
602     }
603 }
604
605 if ($major < 2
606     && !extract_from_file('Configure',
607                           qr/Try to guess additional flags to pick up local libraries/)) {
608     my $mips = extract_from_file('Configure',
609                                  qr!(''\) if (?:\./)?mips; then)!);
610     # This is part of perl-5.001n. It's needed, to add -L/usr/local/lib to the
611     # ld flags if libraries are found there. It shifts the code to set up
612     # libpth earlier, and then adds the code to add libpth entries to ldflags
613     # mips was changed to ./mips in ecfc54246c2a6f42, perl5.000 patch.0g
614     apply_patch(sprintf <<'EOPATCH', $mips);
615 diff --git a/Configure b/Configure
616 index 53649d5..0635a6e 100755
617 --- a/Configure
618 +++ b/Configure
619 @@ -2749,6 +2749,52 @@ EOM
620         ;;
621  esac
622  
623 +: Set private lib path
624 +case "$plibpth" in
625 +'') if ./mips; then
626 +               plibpth="$incpath/usr/lib /usr/local/lib /usr/ccs/lib"
627 +       fi;;
628 +esac
629 +case "$libpth" in
630 +' ') dlist='';;
631 +'') dlist="$plibpth $glibpth";;
632 +*) dlist="$libpth";;
633 +esac
634 +
635 +: Now check and see which directories actually exist, avoiding duplicates
636 +libpth=''
637 +for xxx in $dlist
638 +do
639 +    if $test -d $xxx; then
640 +               case " $libpth " in
641 +               *" $xxx "*) ;;
642 +               *) libpth="$libpth $xxx";;
643 +               esac
644 +    fi
645 +done
646 +$cat <<'EOM'
647 +
648 +Some systems have incompatible or broken versions of libraries.  Among
649 +the directories listed in the question below, please remove any you
650 +know not to be holding relevant libraries, and add any that are needed.
651 +Say "none" for none.
652 +
653 +EOM
654 +case "$libpth" in
655 +'') dflt='none';;
656 +*)
657 +       set X $libpth
658 +       shift
659 +       dflt=${1+"$@"}
660 +       ;;
661 +esac
662 +rp="Directories to use for library searches?"
663 +. ./myread
664 +case "$ans" in
665 +none) libpth=' ';;
666 +*) libpth="$ans";;
667 +esac
668 +
669  : flags used in final linking phase
670  case "$ldflags" in
671  '') if ./venix; then
672 @@ -2765,6 +2811,23 @@ case "$ldflags" in
673         ;;
674  *) dflt="$ldflags";;
675  esac
676 +
677 +: Possible local library directories to search.
678 +loclibpth="/usr/local/lib /opt/local/lib /usr/gnu/lib"
679 +loclibpth="$loclibpth /opt/gnu/lib /usr/GNU/lib /opt/GNU/lib"
680 +
681 +: Try to guess additional flags to pick up local libraries.
682 +for thislibdir in $libpth; do
683 +       case " $loclibpth " in
684 +       *" $thislibdir "*)
685 +               case "$dflt " in 
686 +               "-L$thislibdir ") ;;
687 +               *)  dflt="$dflt -L$thislibdir" ;;
688 +               esac
689 +               ;;
690 +       esac
691 +done
692 +
693  echo " "
694  rp="Any additional ld flags (NOT including libraries)?"
695  . ./myread
696 @@ -2828,52 +2891,6 @@ n) echo "OK, that should do.";;
697  esac
698  $rm -f try try.* core
699  
700 -: Set private lib path
701 -case "$plibpth" in
702 -%s
703 -               plibpth="$incpath/usr/lib /usr/local/lib /usr/ccs/lib"
704 -       fi;;
705 -esac
706 -case "$libpth" in
707 -' ') dlist='';;
708 -'') dlist="$plibpth $glibpth";;
709 -*) dlist="$libpth";;
710 -esac
711 -
712 -: Now check and see which directories actually exist, avoiding duplicates
713 -libpth=''
714 -for xxx in $dlist
715 -do
716 -    if $test -d $xxx; then
717 -               case " $libpth " in
718 -               *" $xxx "*) ;;
719 -               *) libpth="$libpth $xxx";;
720 -               esac
721 -    fi
722 -done
723 -$cat <<'EOM'
724 -
725 -Some systems have incompatible or broken versions of libraries.  Among
726 -the directories listed in the question below, please remove any you
727 -know not to be holding relevant libraries, and add any that are needed.
728 -Say "none" for none.
729 -
730 -EOM
731 -case "$libpth" in
732 -'') dflt='none';;
733 -*)
734 -       set X $libpth
735 -       shift
736 -       dflt=${1+"$@"}
737 -       ;;
738 -esac
739 -rp="Directories to use for library searches?"
740 -. ./myread
741 -case "$ans" in
742 -none) libpth=' ';;
743 -*) libpth="$ans";;
744 -esac
745 -
746  : compute shared library extension
747  case "$so" in
748  '')
749 EOPATCH
750 }
751
752 if ($major < 5 && extract_from_file('Configure',
753                                     qr!if \$cc \$ccflags try\.c -o try >/dev/null 2>&1; then!)) {
754     # Analogous to the more general fix of dfe9444ca7881e71
755     # Without this flags such as -m64 may not be passed to this compile, which
756     # results in a byteorder of '1234' instead of '12345678', which can then
757     # cause crashes.
758     
759     if (extract_from_file('Configure', qr/xxx_prompt=y/)) {
760         # 8e07c86ebc651fe9 or later
761         # ("This is my patch  patch.1n  for perl5.001.")
762         apply_patch(<<'EOPATCH');
763 diff --git a/Configure b/Configure
764 index 62249dd..c5c384e 100755
765 --- a/Configure
766 +++ b/Configure
767 @@ -8247,7 +8247,7 @@ main()
768  }
769  EOCP
770         xxx_prompt=y
771 -       if $cc $ccflags try.c -o try >/dev/null 2>&1 && ./try > /dev/null; then
772 +       if $cc $ccflags $ldflags try.c -o try >/dev/null 2>&1 && ./try > /dev/null; then
773                 dflt=`./try`
774                 case "$dflt" in
775                 [1-4][1-4][1-4][1-4]|12345678|87654321)
776 EOPATCH
777     } else {
778         apply_patch(<<'EOPATCH');
779 diff --git a/Configure b/Configure
780 index 53649d5..f1cd64a 100755
781 --- a/Configure
782 +++ b/Configure
783 @@ -6362,7 +6362,7 @@ main()
784         printf("\n");
785  }
786  EOCP
787 -       if $cc $ccflags try.c -o try >/dev/null 2>&1 ; then
788 +       if $cc $ccflags $ldflags try.c -o try >/dev/null 2>&1 ; then
789                 dflt=`./try`
790                 case "$dflt" in
791                 ????|????????) echo "(The test program ran ok.)";;
792 EOPATCH
793     }
794 }
795
796 if ($major < 8 && !extract_from_file('Configure',
797                                     qr/^\t\tif test ! -t 0; then$/)) {
798     # Before dfe9444ca7881e71, Configure would refuse to run if stdin was not a
799     # tty. With that commit, the tty requirement was dropped for -de and -dE
800     # Commit aaeb8e512e8e9e14 dropped the tty requirement for -S
801     # For those older versions, it's probably easiest if we simply remove the
802     # sanity test.
803     edit_file('Configure', sub {
804                   my $code = shift;
805                   $code =~ s/test ! -t 0/test Perl = rules/;
806                   return $code;
807               });
808 }
809
810 if ($major == 8 || $major == 9) {
811     # Fix symbol detection to that of commit 373dfab3839ca168 if it's any
812     # intermediate version 5129fff43c4fe08c or later, as the intermediate
813     # versions don't work correctly on (at least) Sparc Linux.
814     # 5129fff43c4fe08c adds the first mention of mistrustnm.
815     # 373dfab3839ca168 removes the last mention of lc=""
816     edit_file('Configure', sub {
817                   my $code = shift;
818                   return $code
819                       if $code !~ /\btc="";/; # 373dfab3839ca168 or later
820                   return $code
821                       if $code !~ /\bmistrustnm\b/; # before 5129fff43c4fe08c
822                   my $fixed = <<'EOC';
823
824 : is a C symbol defined?
825 csym='tlook=$1;
826 case "$3" in
827 -v) tf=libc.tmp; tdc="";;
828 -a) tf=libc.tmp; tdc="[]";;
829 *) tlook="^$1\$"; tf=libc.list; tdc="()";;
830 esac;
831 tx=yes;
832 case "$reuseval-$4" in
833 true-) ;;
834 true-*) tx=no; eval "tval=\$$4"; case "$tval" in "") tx=yes;; esac;;
835 esac;
836 case "$tx" in
837 yes)
838         tval=false;
839         if $test "$runnm" = true; then
840                 if $contains $tlook $tf >/dev/null 2>&1; then
841                         tval=true;
842                 elif $test "$mistrustnm" = compile -o "$mistrustnm" = run; then
843                         echo "void *(*(p()))$tdc { extern void *$1$tdc; return &$1; } int main() { if(p()) return(0); else return(1); }"> try.c;
844                         $cc -o try $optimize $ccflags $ldflags try.c >/dev/null 2>&1 $libs && tval=true;
845                         $test "$mistrustnm" = run -a -x try && { $run ./try$_exe >/dev/null 2>&1 || tval=false; };
846                         $rm -f try$_exe try.c core core.* try.core;
847                 fi;
848         else
849                 echo "void *(*(p()))$tdc { extern void *$1$tdc; return &$1; } int main() { if(p()) return(0); else return(1); }"> try.c;
850                 $cc -o try $optimize $ccflags $ldflags try.c $libs >/dev/null 2>&1 && tval=true;
851                 $rm -f try$_exe try.c;
852         fi;
853         ;;
854 *)
855         case "$tval" in
856         $define) tval=true;;
857         *) tval=false;;
858         esac;
859         ;;
860 esac;
861 eval "$2=$tval"'
862
863 EOC
864                   $code =~ s/\n: is a C symbol defined\?\n.*?\neval "\$2=\$tval"'\n\n/$fixed/sm
865                       or die "substitution failed";
866                   return $code;
867               });
868 }
869
870 if ($major < 10 && extract_from_file('Configure', qr/^set malloc\.h i_malloc$/)) {
871     # This is commit 01d07975f7ef0e7d, trimmed, with $compile inlined as
872     # prior to bd9b35c97ad661cc Configure had the malloc.h test before the
873     # definition of $compile.
874     apply_patch(<<'EOPATCH');
875 diff --git a/Configure b/Configure
876 index 3d2e8b9..6ce7766 100755
877 --- a/Configure
878 +++ b/Configure
879 @@ -6743,5 +6743,22 @@ set d_dosuid
880  
881  : see if this is a malloc.h system
882 -set malloc.h i_malloc
883 -eval $inhdr
884 +: we want a real compile instead of Inhdr because some systems have a
885 +: malloc.h that just gives a compile error saying to use stdlib.h instead
886 +echo " "
887 +$cat >try.c <<EOCP
888 +#include <stdlib.h>
889 +#include <malloc.h>
890 +int main () { return 0; }
891 +EOCP
892 +set try
893 +if $cc $optimize $ccflags $ldflags -o try $* try.c $libs > /dev/null 2>&1; then
894 +    echo "<malloc.h> found." >&4
895 +    val="$define"
896 +else
897 +    echo "<malloc.h> NOT found." >&4
898 +    val="$undef"
899 +fi
900 +$rm -f try.c try
901 +set i_malloc
902 +eval $setvar
903  
904 EOPATCH
905 }
906
907 # Cwd.xs added in commit 0d2079faa739aaa9. Cwd.pm moved to ext/ 8 years later
908 # in commit 403f501d5b37ebf0
909 if ($major > 0 && <*/Cwd/Cwd.xs>) {
910     if ($major < 10 && !extract_from_file('Makefile.SH', qr/^extra_dep=''$/)) {
911         # The Makefile.PL for Unicode::Normalize needs
912         # lib/unicore/CombiningClass.pl. Even without a parallel build, we need
913         # a dependency to ensure that it builds. This is a variant of commit
914         # 9f3ef600c170f61e. Putting this for earlier versions gives us a spot
915         # on which to hang the edits below
916         apply_patch(<<'EOPATCH');
917 diff --git a/Makefile.SH b/Makefile.SH
918 index f61d0db..6097954 100644
919 --- a/Makefile.SH
920 +++ b/Makefile.SH
921 @@ -155,10 +155,20 @@ esac
922  
923  : Prepare dependency lists for Makefile.
924  dynamic_list=' '
925 +extra_dep=''
926  for f in $dynamic_ext; do
927      : the dependency named here will never exist
928        base=`echo "$f" | sed 's/.*\///'`
929 -    dynamic_list="$dynamic_list lib/auto/$f/$base.$dlext"
930 +    this_target="lib/auto/$f/$base.$dlext"
931 +    dynamic_list="$dynamic_list $this_target"
932 +
933 +    : Parallel makes reveal that we have some interdependencies
934 +    case $f in
935 +       Math/BigInt/FastCalc) extra_dep="$extra_dep
936 +$this_target: lib/auto/List/Util/Util.$dlext" ;;
937 +       Unicode/Normalize) extra_dep="$extra_dep
938 +$this_target: lib/unicore/CombiningClass.pl" ;;
939 +    esac
940  done
941  
942  static_list=' '
943 @@ -987,2 +997,9 @@ n_dummy $(nonxs_ext):       miniperl$(EXE_EXT) preplibrary $(DYNALOADER) FORCE
944         @$(LDLIBPTH) sh ext/util/make_ext nonxs $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
945 +!NO!SUBS!
946 +
947 +$spitshell >>Makefile <<EOF
948 +$extra_dep
949 +EOF
950 +
951 +$spitshell >>Makefile <<'!NO!SUBS!'
952  
953 EOPATCH
954     }
955     if ($major < 14) {
956         # Commits dc0655f797469c47 and d11a62fe01f2ecb2
957         edit_file('Makefile.SH', sub {
958                       my $code = shift;
959                       foreach my $ext (qw(Encode SDBM_File)) {
960                           next if $code =~ /\b$ext\) extra_dep=/s;
961                           $code =~ s!(\) extra_dep="\$extra_dep
962 \$this_target: .*?" ;;)
963 (    esac
964 )!$1
965         $ext) extra_dep="\$extra_dep
966 \$this_target: lib/auto/Cwd/Cwd.\$dlext" ;;
967 $2!;
968                       }
969                       return $code;
970                   });
971     }
972 }
973 if ($major == 7) {
974     # Remove commits 9fec149bb652b6e9 and 5bab1179608f81d8, which add/amend
975     # rules to automatically run regen scripts that rebuild C headers. These
976     # cause problems because a git checkout doesn't preserve relative file
977     # modification times, hence the regen scripts may fire. This will obscure
978     # whether the repository had the correct generated headers checked in.
979     # Also, the dependency rules for running the scripts were not correct,
980     # which could cause spurious re-builds on re-running make, and can cause
981     # complete build failures for a parallel make.
982     if (extract_from_file('Makefile.SH',
983                           qr/Writing it this way gives make a big hint to always run opcode\.pl before/)) {
984         apply_commit('70c6e6715e8fec53');
985     } elsif (extract_from_file('Makefile.SH',
986                                qr/^opcode\.h opnames\.h pp_proto\.h pp\.sym: opcode\.pl$/)) {
987         revert_commit('9fec149bb652b6e9');
988     }
989 }
990
991 # There was a bug in makedepend.SH which was fixed in version 96a8704c.
992 # Symptom was './makedepend: 1: Syntax error: Unterminated quoted string'
993 # Remove this if you're actually bisecting a problem related to makedepend.SH
994 system 'git show blead:makedepend.SH > makedepend.SH </dev/null' and die;
995
996 if ($^O eq 'freebsd') {
997     # There are rather too many version-specific FreeBSD hints fixes to patch
998     # individually. Also, more than once the FreeBSD hints file has been
999     # written in what turned out to be a rather non-future-proof style,
1000     # with case statements treating the most recent version as the exception,
1001     # instead of treating previous versions' behaviour explicitly and changing
1002     # the default to cater for the current behaviour. (As strangely, future
1003     # versions inherit the current behaviour.)
1004     system 'git show blead:hints/freebsd.sh > hints/freebsd.sh </dev/null'
1005       and die;
1006 } elsif ($^O eq 'darwin') {
1007     if ($major < 8) {
1008         my $faking_it;
1009         # We can't build on darwin without some of the data in the hints file.
1010         foreach ('ext/DynaLoader/dl_dyld.xs', 'hints/darwin.sh') {
1011             next if -f $_;
1012             ++$faking_it;
1013             # Probably less surprising to use the earliest version of
1014             # hints/darwin.sh and then edit in place just below, than use
1015             # blead's version, as that would create a discontinuity at
1016             # f556e5b971932902 - before it, hints bugs would be "fixed", after
1017             # it they'd resurface. This way, we should give the illusion of
1018             # monotonic bug fixing.
1019             system "git show f556e5b971932902:$_ >$_"
1020                 and die "while attempting to extract $_";
1021         }
1022         if ($faking_it) {
1023             apply_patch(<<'EOPATCH');
1024 diff -u a/ext/DynaLoader/dl_dyld.xs~ a/ext/DynaLoader/dl_dyld.xs
1025 --- a/ext/DynaLoader/dl_dyld.xs~        2011-10-11 21:41:27.000000000 +0100
1026 +++ b/ext/DynaLoader/dl_dyld.xs 2011-10-11 21:42:20.000000000 +0100
1027 @@ -41,6 +41,35 @@
1028  #include "perl.h"
1029  #include "XSUB.h"
1030  
1031 +#ifndef pTHX
1032 +#  define pTHX         void
1033 +#  define pTHX_
1034 +#endif
1035 +#ifndef aTHX
1036 +#  define aTHX
1037 +#  define aTHX_
1038 +#endif
1039 +#ifndef dTHX
1040 +#  define dTHXa(a)     extern int Perl___notused(void)
1041 +#  define dTHX         extern int Perl___notused(void)
1042 +#endif
1043 +
1044 +#ifndef Perl_form_nocontext
1045 +#  define Perl_form_nocontext form
1046 +#endif
1047 +
1048 +#ifndef Perl_warn_nocontext
1049 +#  define Perl_warn_nocontext warn
1050 +#endif
1051 +
1052 +#ifndef PTR2IV
1053 +#  define PTR2IV(p)    (IV)(p)
1054 +#endif
1055 +
1056 +#ifndef get_av
1057 +#  define get_av perl_get_av
1058 +#endif
1059 +
1060  #define DL_LOADONCEONLY
1061  
1062  #include "dlutils.c"   /* SaveError() etc      */
1063 @@ -185,7 +191,7 @@
1064      CODE:
1065      DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
1066      if (flags & 0x01)
1067 -       Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
1068 +       Perl_warn_nocontext("Can't make loaded symbols global on this platform while loading %s",filename);
1069      RETVAL = dlopen(filename, mode) ;
1070      DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL));
1071      ST(0) = sv_newmortal() ;
1072 EOPATCH
1073             if ($major < 4 && !extract_from_file('util.c', qr/^form/m)) {
1074                 apply_patch(<<'EOPATCH');
1075 diff -u a/ext/DynaLoader/dl_dyld.xs~ a/ext/DynaLoader/dl_dyld.xs
1076 --- a/ext/DynaLoader/dl_dyld.xs~        2011-10-11 21:56:25.000000000 +0100
1077 +++ b/ext/DynaLoader/dl_dyld.xs 2011-10-11 22:00:00.000000000 +0100
1078 @@ -60,6 +60,18 @@
1079  #  define get_av perl_get_av
1080  #endif
1081  
1082 +static char *
1083 +form(char *pat, ...)
1084 +{
1085 +    char *retval;
1086 +    va_list args;
1087 +    va_start(args, pat);
1088 +    vasprintf(&retval, pat, &args);
1089 +    va_end(args);
1090 +    SAVEFREEPV(retval);
1091 +    return retval;
1092 +}
1093 +
1094  #define DL_LOADONCEONLY
1095  
1096  #include "dlutils.c"   /* SaveError() etc      */
1097 EOPATCH
1098             }
1099         }
1100
1101         edit_file('hints/darwin.sh', sub {
1102                       my $code = shift;
1103                       # Part of commit 8f4f83badb7d1ba9, which mostly undoes
1104                       # commit 0511a818910f476c.
1105                       $code =~ s/^cppflags='-traditional-cpp';$/cppflags="\${cppflags} -no-cpp-precomp"/m;
1106                       # commit 14c11978e9b52e08/803bb6cc74d36a3f
1107                       # Without this, code in libperl.bundle links against op.o
1108                       # in preference to opmini.o on the linker command line,
1109                       # and hence miniperl tries to use File::Glob instead of
1110                       # csh
1111                       $code =~ s/^(lddlflags=)/ldflags="\${ldflags} -flat_namespace"\n$1/m;
1112                       # f556e5b971932902 also patches Makefile.SH with some
1113                       # special case code to deal with useshrplib for darwin.
1114                       # Given that post 5.8.0 the darwin hints default was
1115                       # changed to false, and it would be very complex to splice
1116                       # in that code in various versions of Makefile.SH back
1117                       # to 5.002, lets just turn it off.
1118                       $code =~ s/^useshrplib='true'/useshrplib='false'/m
1119                           if $faking_it;
1120                       return $code;
1121                   });
1122     }
1123 } elsif ($^O eq 'netbsd') {
1124     if ($major < 6) {
1125         # These are part of commit 099685bc64c7dbce
1126         edit_file('hints/netbsd.sh', sub {
1127                       my $code = shift;
1128                       my $fixed = <<'EOC';
1129 case "$osvers" in
1130 0.9|0.8*)
1131         usedl="$undef"
1132         ;;
1133 *)
1134         if [ -f /usr/libexec/ld.elf_so ]; then
1135                 d_dlopen=$define
1136                 d_dlerror=$define
1137                 ccdlflags="-Wl,-E -Wl,-R${PREFIX}/lib $ccdlflags"
1138                 cccdlflags="-DPIC -fPIC $cccdlflags"
1139                 lddlflags="--whole-archive -shared $lddlflags"
1140         elif [ "`uname -m`" = "pmax" ]; then
1141 # NetBSD 1.3 and 1.3.1 on pmax shipped an `old' ld.so, which will not work.
1142                 d_dlopen=$undef
1143         elif [ -f /usr/libexec/ld.so ]; then
1144                 d_dlopen=$define
1145                 d_dlerror=$define
1146                 ccdlflags="-Wl,-R${PREFIX}/lib $ccdlflags"
1147 # we use -fPIC here because -fpic is *NOT* enough for some of the
1148 # extensions like Tk on some netbsd platforms (the sparc is one)
1149                 cccdlflags="-DPIC -fPIC $cccdlflags"
1150                 lddlflags="-Bforcearchive -Bshareable $lddlflags"
1151         else
1152                 d_dlopen=$undef
1153         fi
1154         ;;
1155 esac
1156 EOC
1157                       $code =~ s/^case "\$osvers" in\n0\.9\|0\.8.*?^esac\n/$fixed/ms;
1158                       return $code;
1159                   });
1160         if (!extract_from_file('unixish.h',
1161                                qr/defined\(NSIG\).*defined\(__NetBSD__\)/)) {
1162             apply_patch(<<'EOPATCH')
1163 diff --git a/unixish.h b/unixish.h
1164 index 2a6cbcd..eab2de1 100644
1165 --- a/unixish.h
1166 +++ b/unixish.h
1167 @@ -89,7 +89,7 @@
1168   */
1169  /* #define ALTERNATE_SHEBANG "#!" / **/
1170  
1171 -#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
1172 +#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) || defined(__NetBSD__)
1173  # include <signal.h>
1174  #endif
1175  
1176 EOPATCH
1177         }
1178     }
1179 } elsif ($^O eq 'openbsd') {
1180     if (!-f 'hints/openbsd.sh') {
1181         system 'git show 43051805d53a3e4c:hints/openbsd.sh > hints/openbsd.sh'
1182           and die;
1183     }
1184
1185     if ($major < 8) {
1186         my $which = extract_from_file('hints/openbsd.sh',
1187                                       qr/# from (2\.8|3\.1) onwards/,
1188                                       '');
1189         if ($which eq '') {
1190             my $was = extract_from_file('hints/openbsd.sh',
1191                                         qr/(lddlflags="(?:-Bforcearchive )?-Bshareable)/);
1192             # This is commit 154d43cbcf57271c and parts of 5c75dbfa77b0949c
1193             # and 29b5585702e5e025
1194             apply_patch(sprintf <<'EOPATCH', $was);
1195 diff --git a/hints/openbsd.sh b/hints/openbsd.sh
1196 index a7d8bf2..5b79709 100644
1197 --- a/hints/openbsd.sh
1198 +++ b/hints/openbsd.sh
1199 @@ -37,7 +37,25 @@ OpenBSD.alpha|OpenBSD.mips|OpenBSD.powerpc|OpenBSD.vax)
1200         # we use -fPIC here because -fpic is *NOT* enough for some of the
1201         # extensions like Tk on some OpenBSD platforms (ie: sparc)
1202         cccdlflags="-DPIC -fPIC $cccdlflags"
1203 -       %s $lddlflags"
1204 +       case "$osvers" in
1205 +       [01].*|2.[0-7]|2.[0-7].*)
1206 +               lddlflags="-Bshareable $lddlflags"
1207 +               ;;
1208 +       2.[8-9]|3.0)
1209 +               ld=${cc:-cc}
1210 +               lddlflags="-shared -fPIC $lddlflags"
1211 +               ;;
1212 +       *) # from 3.1 onwards
1213 +               ld=${cc:-cc}
1214 +               lddlflags="-shared -fPIC $lddlflags"
1215 +               libswanted=`echo $libswanted | sed 's/ dl / /'`
1216 +               ;;
1217 +       esac
1218 +
1219 +       # We need to force ld to export symbols on ELF platforms.
1220 +       # Without this, dlopen() is crippled.
1221 +       ELF=`${cc:-cc} -dM -E - </dev/null | grep __ELF__`
1222 +       test -n "$ELF" && ldflags="-Wl,-E $ldflags"
1223         ;;
1224  esac
1225  
1226 EOPATCH
1227         } elsif ($which eq '2.8') {
1228             # This is parts of 5c75dbfa77b0949c and 29b5585702e5e025, and
1229             # possibly eb9cd59d45ad2908
1230             my $was = extract_from_file('hints/openbsd.sh',
1231                                         qr/lddlflags="(-shared(?: -fPIC)?) \$lddlflags"/);
1232
1233             apply_patch(sprintf <<'EOPATCH', $was);
1234 --- a/hints/openbsd.sh  2011-10-21 17:25:20.000000000 +0200
1235 +++ b/hints/openbsd.sh  2011-10-21 16:58:43.000000000 +0200
1236 @@ -44,11 +44,21 @@
1237         [01].*|2.[0-7]|2.[0-7].*)
1238                 lddlflags="-Bshareable $lddlflags"
1239                 ;;
1240 -       *) # from 2.8 onwards
1241 +       2.[8-9]|3.0)
1242                 ld=${cc:-cc}
1243 -               lddlflags="%s $lddlflags"
1244 +               lddlflags="-shared -fPIC $lddlflags"
1245 +               ;;
1246 +       *) # from 3.1 onwards
1247 +               ld=${cc:-cc}
1248 +               lddlflags="-shared -fPIC $lddlflags"
1249 +               libswanted=`echo $libswanted | sed 's/ dl / /'`
1250                 ;;
1251         esac
1252 +
1253 +       # We need to force ld to export symbols on ELF platforms.
1254 +       # Without this, dlopen() is crippled.
1255 +       ELF=`${cc:-cc} -dM -E - </dev/null | grep __ELF__`
1256 +       test -n "$ELF" && ldflags="-Wl,-E $ldflags"
1257         ;;
1258  esac
1259  
1260 EOPATCH
1261         } elsif ($which eq '3.1'
1262                 && !extract_from_file('hints/openbsd.sh',
1263                                      qr/We need to force ld to export symbols on ELF platforms/)) {
1264             # This is part of 29b5585702e5e025
1265             apply_patch(<<'EOPATCH');
1266 diff --git a/hints/openbsd.sh b/hints/openbsd.sh
1267 index c6b6bc9..4839d04 100644
1268 --- a/hints/openbsd.sh
1269 +++ b/hints/openbsd.sh
1270 @@ -54,6 +54,11 @@ alpha-2.[0-8]|mips-*|vax-*|powerpc-2.[0-7]|m88k-*)
1271                 libswanted=`echo $libswanted | sed 's/ dl / /'`
1272                 ;;
1273         esac
1274 +
1275 +       # We need to force ld to export symbols on ELF platforms.
1276 +       # Without this, dlopen() is crippled.
1277 +       ELF=`${cc:-cc} -dM -E - </dev/null | grep __ELF__`
1278 +       test -n "$ELF" && ldflags="-Wl,-E $ldflags"
1279         ;;
1280  esac
1281  
1282 EOPATCH
1283         }
1284     }
1285     if ($major < 4) {
1286         my $bad;
1287         # Need changes from commit a6e633defa583ad5.
1288         # Commits c07a80fdfe3926b5 and f82b3d4130164d5f changed the same part
1289         # of perl.h
1290
1291         if (extract_from_file('perl.h',
1292                               qr/^#ifdef HAS_GETPGRP2$/)) {
1293             $bad = <<'EOBAD';
1294 ***************
1295 *** 57,71 ****
1296   #define TAINT_PROPER(s)       if (tainting) taint_proper(no_security, s)
1297   #define TAINT_ENV()   if (tainting) taint_env()
1298   
1299 ! #ifdef HAS_GETPGRP2
1300 ! #   ifndef HAS_GETPGRP
1301 ! #     define HAS_GETPGRP
1302 ! #   endif
1303 ! #endif
1304
1305 ! #ifdef HAS_SETPGRP2
1306 ! #   ifndef HAS_SETPGRP
1307 ! #     define HAS_SETPGRP
1308 ! #   endif
1309   #endif
1310   
1311 EOBAD
1312         } elsif (extract_from_file('perl.h',
1313                                    qr/Gack, you have one but not both of getpgrp2/)) {
1314             $bad = <<'EOBAD';
1315 ***************
1316 *** 56,76 ****
1317   #define TAINT_PROPER(s)       if (tainting) taint_proper(no_security, s)
1318   #define TAINT_ENV()   if (tainting) taint_env()
1319   
1320 ! #if defined(HAS_GETPGRP2) && defined(HAS_SETPGRP2)
1321 ! #   define getpgrp getpgrp2
1322 ! #   define setpgrp setpgrp2
1323 ! #   ifndef HAS_GETPGRP
1324 ! #     define HAS_GETPGRP
1325 ! #   endif
1326 ! #   ifndef HAS_SETPGRP
1327 ! #     define HAS_SETPGRP
1328 ! #   endif
1329 ! #   ifndef USE_BSDPGRP
1330 ! #     define USE_BSDPGRP
1331 ! #   endif
1332 ! #else
1333 ! #   if defined(HAS_GETPGRP2) || defined(HAS_SETPGRP2)
1334 !       #include "Gack, you have one but not both of getpgrp2() and setpgrp2()."
1335 ! #   endif
1336   #endif
1337   
1338 EOBAD
1339         } elsif (extract_from_file('perl.h',
1340                                    qr/^#ifdef USE_BSDPGRP$/)) {
1341             $bad = <<'EOBAD'
1342 ***************
1343 *** 91,116 ****
1344   #define TAINT_PROPER(s)       if (tainting) taint_proper(no_security, s)
1345   #define TAINT_ENV()   if (tainting) taint_env()
1346   
1347 ! #ifdef USE_BSDPGRP
1348 ! #   ifdef HAS_GETPGRP
1349 ! #       define BSD_GETPGRP(pid) getpgrp((pid))
1350 ! #   endif
1351 ! #   ifdef HAS_SETPGRP
1352 ! #       define BSD_SETPGRP(pid, pgrp) setpgrp((pid), (pgrp))
1353 ! #   endif
1354 ! #else
1355 ! #   ifdef HAS_GETPGRP2
1356 ! #       define BSD_GETPGRP(pid) getpgrp2((pid))
1357 ! #       ifndef HAS_GETPGRP
1358 ! #         define HAS_GETPGRP
1359 ! #     endif
1360 ! #   endif
1361 ! #   ifdef HAS_SETPGRP2
1362 ! #       define BSD_SETPGRP(pid, pgrp) setpgrp2((pid), (pgrp))
1363 ! #       ifndef HAS_SETPGRP
1364 ! #         define HAS_SETPGRP
1365 ! #     endif
1366 ! #   endif
1367   #endif
1368   
1369   #ifndef _TYPES_               /* If types.h defines this it's easy. */
1370 EOBAD
1371         }
1372         if ($bad) {
1373             apply_patch(<<"EOPATCH");
1374 *** a/perl.h    2011-10-21 09:46:12.000000000 +0200
1375 --- b/perl.h    2011-10-21 09:46:12.000000000 +0200
1376 $bad--- 91,144 ----
1377   #define TAINT_PROPER(s)       if (tainting) taint_proper(no_security, s)
1378   #define TAINT_ENV()   if (tainting) taint_env()
1379   
1380 ! /* XXX All process group stuff is handled in pp_sys.c.  Should these 
1381 !    defines move there?  If so, I could simplify this a lot. --AD  9/96.
1382 ! */
1383 ! /* Process group stuff changed from traditional BSD to POSIX.
1384 !    perlfunc.pod documents the traditional BSD-style syntax, so we'll
1385 !    try to preserve that, if possible.
1386 ! */
1387 ! #ifdef HAS_SETPGID
1388 ! #  define BSD_SETPGRP(pid, pgrp)      setpgid((pid), (pgrp))
1389 ! #else
1390 ! #  if defined(HAS_SETPGRP) && defined(USE_BSD_SETPGRP)
1391 ! #    define BSD_SETPGRP(pid, pgrp)    setpgrp((pid), (pgrp))
1392 ! #  else
1393 ! #    ifdef HAS_SETPGRP2  /* DG/UX */
1394 ! #      define BSD_SETPGRP(pid, pgrp)  setpgrp2((pid), (pgrp))
1395 ! #    endif
1396 ! #  endif
1397 ! #endif
1398 ! #if defined(BSD_SETPGRP) && !defined(HAS_SETPGRP)
1399 ! #  define HAS_SETPGRP  /* Well, effectively it does . . . */
1400 ! #endif
1401
1402 ! /* getpgid isn't POSIX, but at least Solaris and Linux have it, and it makes
1403 !     our life easier :-) so we'll try it.
1404 ! */
1405 ! #ifdef HAS_GETPGID
1406 ! #  define BSD_GETPGRP(pid)            getpgid((pid))
1407 ! #else
1408 ! #  if defined(HAS_GETPGRP) && defined(USE_BSD_GETPGRP)
1409 ! #    define BSD_GETPGRP(pid)          getpgrp((pid))
1410 ! #  else
1411 ! #    ifdef HAS_GETPGRP2  /* DG/UX */
1412 ! #      define BSD_GETPGRP(pid)                getpgrp2((pid))
1413 ! #    endif
1414 ! #  endif
1415 ! #endif
1416 ! #if defined(BSD_GETPGRP) && !defined(HAS_GETPGRP)
1417 ! #  define HAS_GETPGRP  /* Well, effectively it does . . . */
1418 ! #endif
1419
1420 ! /* These are not exact synonyms, since setpgrp() and getpgrp() may 
1421 !    have different behaviors, but perl.h used to define USE_BSDPGRP
1422 !    (prior to 5.003_05) so some extension might depend on it.
1423 ! */
1424 ! #if defined(USE_BSD_SETPGRP) || defined(USE_BSD_GETPGRP)
1425 ! #  ifndef USE_BSDPGRP
1426 ! #    define USE_BSDPGRP
1427 ! #  endif
1428   #endif
1429   
1430   #ifndef _TYPES_               /* If types.h defines this it's easy. */
1431 EOPATCH
1432         }
1433     }
1434     if ($major < 3 && !extract_from_file('pp_sys.c', qr/BSD_GETPGRP/)) {
1435         # Part of commit c3293030fd1b7489
1436         apply_patch(<<'EOPATCH');
1437 diff --git a/pp_sys.c b/pp_sys.c
1438 index 4608a2a..f0c9d1d 100644
1439 --- a/pp_sys.c
1440 +++ b/pp_sys.c
1441 @@ -2903,8 +2903,8 @@ PP(pp_getpgrp)
1442         pid = 0;
1443      else
1444         pid = SvIVx(POPs);
1445 -#ifdef USE_BSDPGRP
1446 -    value = (I32)getpgrp(pid);
1447 +#ifdef BSD_GETPGRP
1448 +    value = (I32)BSD_GETPGRP(pid);
1449  #else
1450      if (pid != 0)
1451         DIE("POSIX getpgrp can't take an argument");
1452 @@ -2933,8 +2933,8 @@ PP(pp_setpgrp)
1453      }
1454  
1455      TAINT_PROPER("setpgrp");
1456 -#ifdef USE_BSDPGRP
1457 -    SETi( setpgrp(pid, pgrp) >= 0 );
1458 +#ifdef BSD_SETPGRP
1459 +    SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
1460  #else
1461      if ((pgrp != 0) || (pid != 0)) {
1462         DIE("POSIX setpgrp can't take an argument");
1463 EOPATCH
1464     }
1465 } elsif ($^O eq 'linux') {
1466     if ($major < 1) {
1467         # sparc linux seems to need the -Dbool=char -DHAS_BOOL part of
1468         # perl5.000 patch.0n: [address Configure and build issues]
1469         edit_file('hints/linux.sh', sub {
1470                       my $code = shift;
1471                       $code =~ s!-I/usr/include/bsd!-Dbool=char -DHAS_BOOL!g;
1472                       return $code;
1473                   });
1474     }
1475
1476     if ($major <= 9) {
1477         if (`uname -sm` =~ qr/^Linux sparc/) {
1478             if (extract_from_file('hints/linux.sh', qr/sparc-linux/)) {
1479                 # Be sure to use -fPIC not -fpic on Linux/SPARC
1480                 apply_commit('f6527d0ef0c13ad4');
1481             } elsif(!extract_from_file('hints/linux.sh', qr/^sparc-linux\)$/)) {
1482                 my $fh = open_or_die('hints/linux.sh', '>>');
1483                 print $fh <<'EOT' or die $!;
1484
1485 case "`uname -m`" in
1486 sparc*)
1487         case "$cccdlflags" in
1488         *-fpic*) cccdlflags="`echo $cccdlflags|sed 's/-fpic/-fPIC/'`" ;;
1489         *)       cccdlflags="$cccdlflags -fPIC" ;;
1490         esac
1491         ;;
1492 esac
1493 EOT
1494                 close_or_die($fh);
1495             }
1496         }
1497     }
1498 }
1499
1500 if ($major < 10) {
1501     if (!extract_from_file('ext/DB_File/DB_File.xs',
1502                            qr!^#else /\* Berkeley DB Version > 2 \*/$!)) {
1503         # This DB_File.xs is really too old to patch up.
1504         # Skip DB_File, unless we're invoked with an explicit -Unoextensions
1505         if (!exists $defines{noextensions}) {
1506             $defines{noextensions} = 'DB_File';
1507         } elsif (defined $defines{noextensions}) {
1508             $defines{noextensions} .= ' DB_File';
1509         }
1510     } elsif (!extract_from_file('ext/DB_File/DB_File.xs',
1511                                 qr/^#ifdef AT_LEAST_DB_4_1$/)) {
1512         # This line is changed by commit 3245f0580c13b3ab
1513         my $line = extract_from_file('ext/DB_File/DB_File.xs',
1514                                      qr/^(        status = \(?RETVAL->dbp->open\)?\(RETVAL->dbp, name, NULL, RETVAL->type, $)/);
1515         apply_patch(<<"EOPATCH");
1516 diff --git a/ext/DB_File/DB_File.xs b/ext/DB_File/DB_File.xs
1517 index 489ba96..fba8ded 100644
1518 --- a/ext/DB_File/DB_File.xs
1519 +++ b/ext/DB_File/DB_File.xs
1520 \@\@ -183,4 +187,8 \@\@
1521  #endif
1522  
1523 +#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
1524 +#    define AT_LEAST_DB_4_1
1525 +#endif
1526 +
1527  /* map version 2 features & constants onto their version 1 equivalent */
1528  
1529 \@\@ -1334,7 +1419,12 \@\@ SV *   sv ;
1530  #endif
1531  
1532 +#ifdef AT_LEAST_DB_4_1
1533 +        status = (RETVAL->dbp->open)(RETVAL->dbp, NULL, name, NULL, RETVAL->type, 
1534 +                               Flags, mode) ; 
1535 +#else
1536  $line
1537                                 Flags, mode) ; 
1538 +#endif
1539         /* printf("open returned %d %s\\n", status, db_strerror(status)) ; */
1540  
1541 EOPATCH
1542     }
1543 }
1544
1545 # if Encode is not needed for the test, you can speed up the bisect by
1546 # excluding it from the runs with -Dnoextensions=Encode
1547 # ccache is an easy win. Remove it if it causes problems.
1548 # Commit 1cfa4ec74d4933da adds ignore_versioned_solibs to Configure, and sets it
1549 # to true in hints/linux.sh
1550 # On dromedary, from that point on, Configure (by default) fails to find any
1551 # libraries, because it scans /usr/local/lib /lib /usr/lib, which only contain
1552 # versioned libraries. Without -lm, the build fails.
1553 # Telling /usr/local/lib64 /lib64 /usr/lib64 works from that commit onwards,
1554 # until commit faae14e6e968e1c0 adds it to the hints.
1555 # However, prior to 1cfa4ec74d4933da telling Configure the truth doesn't work,
1556 # because it will spot versioned libraries, pass them to the compiler, and then
1557 # bail out pretty early on. Configure won't let us override libswanted, but it
1558 # will let us override the entire libs list.
1559
1560 unless (extract_from_file('Configure', 'ignore_versioned_solibs')) {
1561     # Before 1cfa4ec74d4933da, so force the libs list.
1562
1563     my @libs;
1564     # This is the current libswanted list from Configure, less the libs removed
1565     # by current hints/linux.sh
1566     foreach my $lib (qw(sfio socket inet nsl nm ndbm gdbm dbm db malloc dl dld
1567                         ld sun m crypt sec util c cposix posix ucb BSD)) {
1568         foreach my $dir (@paths) {
1569             next unless -f "$dir/lib$lib.so";
1570             push @libs, "-l$lib";
1571             last;
1572         }
1573     }
1574     $defines{libs} = \@libs unless exists $defines{libs};
1575 }
1576
1577 $defines{usenm} = undef
1578     if $major < 2 && !exists $defines{usenm};
1579
1580 my (@missing, @created_dirs);
1581
1582 if ($options{'force-manifest'}) {
1583     my $fh = open_or_die('MANIFEST');
1584     while (<$fh>) {
1585         next unless /^(\S+)/;
1586         # -d is special case needed (at least) between 27332437a2ed1941 and
1587         # bf3d9ec563d25054^ inclusive, as manifest contains ext/Thread/Thread
1588         push @missing, $1
1589             unless -f $1 || -d $1;
1590     }
1591     close_or_die($fh);
1592
1593     foreach my $pathname (@missing) {
1594         my @parts = split '/', $pathname;
1595         my $leaf = pop @parts;
1596         my $path = '.';
1597         while (@parts) {
1598             $path .= '/' . shift @parts;
1599             next if -d $path;
1600             mkdir $path, 0700 or die "Can't create $path: $!";
1601             unshift @created_dirs, $path;
1602         }
1603         $fh = open_or_die($pathname, '>');
1604         close_or_die($fh);
1605         chmod 0, $pathname or die "Can't chmod 0 $pathname: $!";
1606     }
1607 }
1608
1609 my @ARGS = '-dEs';
1610 foreach my $key (sort keys %defines) {
1611     my $val = $defines{$key};
1612     if (ref $val) {
1613         push @ARGS, "-D$key=@$val";
1614     } elsif (!defined $val) {
1615         push @ARGS, "-U$key";
1616     } elsif (!length $val) {
1617         push @ARGS, "-D$key";
1618     } else {
1619         $val = "" if $val eq "\0";
1620         push @ARGS, "-D$key=$val";
1621     }
1622 }
1623 push @ARGS, map {"-A$_"} @{$options{A}};
1624
1625 # </dev/null because it seems that some earlier versions of Configure can
1626 # call commands in a way that now has them reading from stdin (and hanging)
1627 my $pid = fork;
1628 die "Can't fork: $!" unless defined $pid;
1629 if (!$pid) {
1630     open STDIN, '<', '/dev/null';
1631     # If a file in MANIFEST is missing, Configure asks if you want to
1632     # continue (the default being 'n'). With stdin closed or /dev/null,
1633     # it exits immediately and the check for config.sh below will skip.
1634     exec './Configure', @ARGS;
1635     die "Failed to start Configure: $!";
1636 }
1637 waitpid $pid, 0
1638     or die "wait for Configure, pid $pid failed: $!";
1639
1640 # Emulate noextensions if Configure doesn't support it.
1641 if (-f 'config.sh') {
1642     if ($major < 10 && $defines{noextensions}) {
1643         edit_file('config.sh', sub {
1644                       my @lines = split /\n/, shift;
1645                       my @ext = split /\s+/, $defines{noextensions};
1646                       foreach (@lines) {
1647                           next unless /^extensions=/ || /^dynamic_ext/;
1648                           foreach my $ext (@ext) {
1649                               s/\b$ext( )?\b/$1/;
1650                           }
1651                       }
1652                       return join "\n", @lines;
1653                   });
1654     }
1655     if ($major < 4 && !extract_from_file('config.sh', qr/^trnl=/)) {
1656         # This seems to be necessary to avoid makedepend becoming confused,
1657         # and hanging on stdin. Seems that the code after
1658         # make shlist || ...here... is never run.
1659         edit_file('makedepend.SH', sub {
1660                       my $code = shift;
1661                       $code =~ s/^trnl='\$trnl'$/trnl='\\n'/m;
1662                       return $code;
1663                   });
1664     }
1665
1666     system './Configure -S </dev/null' and die;
1667 }
1668
1669 if ($target =~ /config\.s?h/) {
1670     match_and_exit($target) if $match && -f $target;
1671     report_and_exit(!-f $target, 'could build', 'could not build', $target)
1672         if $options{'test-build'};
1673
1674     my $ret = system @ARGV;
1675     report_and_exit($ret, 'zero exit from', 'non-zero exit from', "@ARGV");
1676 } elsif (!-f 'config.sh') {
1677     # Skip if something went wrong with Configure
1678
1679     skip('could not build config.sh');
1680 }
1681
1682 # This is probably way too paranoid:
1683 if (@missing) {
1684     my @errors;
1685     require Fcntl;
1686     foreach my $file (@missing) {
1687         my (undef, undef, $mode, undef, undef, undef, undef, $size)
1688             = stat $file;
1689         if (!defined $mode) {
1690             push @errors, "Added file $file has been deleted by Configure";
1691             next;
1692         }
1693         if (Fcntl::S_IMODE($mode) != 0) {
1694             push @errors,
1695                 sprintf 'Added file %s had mode changed by Configure to %03o',
1696                     $file, $mode;
1697         }
1698         if ($size != 0) {
1699             push @errors,
1700                 "Added file $file had sized changed by Configure to $size";
1701         }
1702         unlink $file or die "Can't unlink $file: $!";
1703     }
1704     foreach my $dir (@created_dirs) {
1705         rmdir $dir or die "Can't rmdir $dir: $!";
1706     }
1707     skip("@errors")
1708         if @errors;
1709 }
1710
1711 # Correct makefile for newer GNU gcc
1712 # Only really needed if you comment out the use of blead's makedepend.SH
1713 {
1714     local $^I = "";
1715     local @ARGV = qw(makefile x2p/makefile);
1716     while (<>) {
1717         print unless /<(?:built-in|command|stdin)/;
1718     }
1719 }
1720
1721 if ($major == 2 && extract_from_file('perl.c', qr/^     fclose\(e_fp\);$/)) {
1722     # need to patch perl.c to avoid calling fclose() twice on e_fp when using -e
1723     # This diff is part of commit ab821d7fdc14a438. The second close was
1724     # introduced with perl-5.002, commit a5f75d667838e8e7
1725     # Might want a6c477ed8d4864e6 too, for the corresponding change to pp_ctl.c
1726     # (likely without this, eval will have "fun")
1727     apply_patch(<<'EOPATCH');
1728 diff --git a/perl.c b/perl.c
1729 index 03c4d48..3c814a2 100644
1730 --- a/perl.c
1731 +++ b/perl.c
1732 @@ -252,6 +252,7 @@ setuid perl scripts securely.\n");
1733  #ifndef VMS  /* VMS doesn't have environ array */
1734      origenviron = environ;
1735  #endif
1736 +    e_tmpname = Nullch;
1737  
1738      if (do_undump) {
1739  
1740 @@ -405,6 +406,7 @@ setuid perl scripts securely.\n");
1741      if (e_fp) {
1742         if (Fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
1743             croak("Can't write to temp file for -e: %s", Strerror(errno));
1744 +       e_fp = Nullfp;
1745         argc++,argv--;
1746         scriptname = e_tmpname;
1747      }
1748 @@ -470,10 +472,10 @@ setuid perl scripts securely.\n");
1749      curcop->cop_line = 0;
1750      curstash = defstash;
1751      preprocess = FALSE;
1752 -    if (e_fp) {
1753 -       fclose(e_fp);
1754 -       e_fp = Nullfp;
1755 +    if (e_tmpname) {
1756         (void)UNLINK(e_tmpname);
1757 +       Safefree(e_tmpname);
1758 +       e_tmpname = Nullch;
1759      }
1760  
1761      /* now that script is parsed, we can modify record separator */
1762 @@ -1369,7 +1371,7 @@ SV *sv;
1763         scriptname = xfound;
1764      }
1765  
1766 -    origfilename = savepv(e_fp ? "-e" : scriptname);
1767 +    origfilename = savepv(e_tmpname ? "-e" : scriptname);
1768      curcop->cop_filegv = gv_fetchfile(origfilename);
1769      if (strEQ(origfilename,"-"))
1770         scriptname = "";
1771
1772 EOPATCH
1773 }
1774
1775 if (($major >= 7 || $major <= 9) && $^O eq 'openbsd'
1776     && `uname -m` eq "sparc64\n"
1777     # added in 2000 by commit cb434fcc98ac25f5:
1778     && extract_from_file('regexec.c',
1779                          qr!/\* No need to save/restore up to this paren \*/!)
1780     # re-indented in 2006 by commit 95b2444054382532:
1781     && extract_from_file('regexec.c', qr/^\t\tCURCUR cc;$/)) {
1782     # Need to work around a bug in (at least) OpenBSD's 4.6's sparc64 compiler
1783     # ["gcc (GCC) 3.3.5 (propolice)"]. Between commits 3ec562b0bffb8b8b (2002)
1784     # and 1a4fad37125bac3e^ (2005) the darling thing fails to compile any code
1785     # for the statement cc.oldcc = PL_regcc;
1786     # If you refactor the code to "fix" that, or force the issue using set in
1787     # the debugger, the stack smashing detection code fires on return from
1788     # S_regmatch(). Turns out that the compiler doesn't allocate any (or at
1789     # least enough) space for cc.
1790     # Restore the "uninitialised" value for cc before function exit, and the
1791     # stack smashing code is placated.
1792     # "Fix" 3ec562b0bffb8b8b (which changes the size of auto variables used
1793     # elsewhere in S_regmatch), and the crash is visible back to
1794     # bc517b45fdfb539b (which also changes buffer sizes). "Unfix"
1795     # 1a4fad37125bac3e and the crash is visible until 5b47454deb66294b.
1796     # Problem goes away if you compile with -O, or hack the code as below.
1797     #
1798     # Hence this turns out to be a bug in (old) gcc. Not a security bug we
1799     # still need to fix.
1800     apply_patch(<<'EOPATCH');
1801 diff --git a/regexec.c b/regexec.c
1802 index 900b491..6251a0b 100644
1803 --- a/regexec.c
1804 +++ b/regexec.c
1805 @@ -2958,7 +2958,11 @@ S_regmatch(pTHX_ regnode *prog)
1806                                 I,I
1807   *******************************************************************/
1808         case CURLYX: {
1809 -               CURCUR cc;
1810 +           union {
1811 +               CURCUR hack_cc;
1812 +               char hack_buff[sizeof(CURCUR) + 1];
1813 +           } hack;
1814 +#define cc hack.hack_cc
1815                 CHECKPOINT cp = PL_savestack_ix;
1816                 /* No need to save/restore up to this paren */
1817                 I32 parenfloor = scan->flags;
1818 @@ -2983,6 +2987,7 @@ S_regmatch(pTHX_ regnode *prog)
1819                 n = regmatch(PREVOPER(next));   /* start on the WHILEM */
1820                 regcpblow(cp);
1821                 PL_regcc = cc.oldcc;
1822 +#undef cc
1823                 saySAME(n);
1824             }
1825             /* NOT REACHED */
1826 EOPATCH
1827 }
1828
1829 if ($major < 8 && $^O eq 'openbsd'
1830     && !extract_from_file('perl.h', qr/include <unistd\.h>/)) {
1831     # This is part of commit 3f270f98f9305540, applied at a slightly different
1832     # location in perl.h, where the context is stable back to 5.000
1833     apply_patch(<<'EOPATCH');
1834 diff --git a/perl.h b/perl.h
1835 index 9418b52..b8b1a7c 100644
1836 --- a/perl.h
1837 +++ b/perl.h
1838 @@ -496,6 +496,10 @@ register struct op *Perl_op asm(stringify(OP_IN_REGISTER));
1839  #   include <sys/param.h>
1840  #endif
1841  
1842 +/* If this causes problems, set i_unistd=undef in the hint file.  */
1843 +#ifdef I_UNISTD
1844 +#   include <unistd.h>
1845 +#endif
1846  
1847  /* Use all the "standard" definitions? */
1848  #if defined(STANDARD_C) && defined(I_STDLIB)
1849 EOPATCH
1850 }
1851
1852 if ($major == 4 && extract_from_file('scope.c', qr/\(SV\*\)SSPOPINT/)) {
1853     # [PATCH] 5.004_04 +MAINT_TRIAL_1 broken when sizeof(int) != sizeof(void)
1854     # Fixes a bug introduced in 161b7d1635bc830b
1855     apply_commit('9002cb76ec83ef7f');
1856 }
1857
1858 if ($major == 4 && extract_from_file('av.c', qr/AvARRAY\(av\) = 0;/)) {
1859     # Fixes a bug introduced in 1393e20655efb4bc
1860     apply_commit('e1c148c28bf3335b', 'av.c');
1861 }
1862
1863 if ($major == 4 && $^O eq 'linux') {
1864     # Whilst this is fixed properly in f0784f6a4c3e45e1 which provides the
1865     # Configure probe, it's easier to back out the problematic changes made in
1866     # these previous commits:
1867     if (extract_from_file('doio.c',
1868                           qr!^/\* XXX REALLY need metaconfig test \*/$!)) {
1869         revert_commit('4682965a1447ea44', 'doio.c');
1870     }
1871     if (my $token = extract_from_file('doio.c',
1872                                       qr!^#if (defined\(__sun(?:__)?\)) && defined\(__svr4__\) /\* XXX Need metaconfig test \*/$!)) {
1873         my $patch = `git show -R 9b599b2a63d2324d doio.c`;
1874         $patch =~ s/defined\(__sun__\)/$token/g;
1875         apply_patch($patch);
1876     }
1877     if (extract_from_file('doio.c',
1878                           qr!^/\* linux \(and Solaris2\?\) uses :$!)) {
1879         revert_commit('8490252049bf42d3', 'doio.c');
1880     }
1881     if (extract_from_file('doio.c',
1882                           qr/^      unsemds.buf = &semds;$/)) {
1883         revert_commit('8e591e46b4c6543e');
1884     }
1885     if (extract_from_file('doio.c',
1886                           qr!^#ifdef __linux__  /\* XXX Need metaconfig test \*/$!)) {
1887         # Reverts part of commit 3e3baf6d63945cb6
1888         apply_patch(<<'EOPATCH');
1889 diff --git b/doio.c a/doio.c
1890 index 62b7de9..0d57425 100644
1891 --- b/doio.c
1892 +++ a/doio.c
1893 @@ -1333,9 +1331,6 @@ SV **sp;
1894      char *a;
1895      I32 id, n, cmd, infosize, getinfo;
1896      I32 ret = -1;
1897 -#ifdef __linux__       /* XXX Need metaconfig test */
1898 -    union semun unsemds;
1899 -#endif
1900  
1901      id = SvIVx(*++mark);
1902      n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1903 @@ -1364,29 +1359,11 @@ SV **sp;
1904             infosize = sizeof(struct semid_ds);
1905         else if (cmd == GETALL || cmd == SETALL)
1906         {
1907 -#ifdef __linux__       /* XXX Need metaconfig test */
1908 -/* linux uses :
1909 -   int semctl (int semid, int semnun, int cmd, union semun arg)
1910 -
1911 -       union semun {
1912 -            int val;
1913 -            struct semid_ds *buf;
1914 -            ushort *array;
1915 -       };
1916 -*/
1917 -            union semun semds;
1918 -           if (semctl(id, 0, IPC_STAT, semds) == -1)
1919 -#else
1920             struct semid_ds semds;
1921             if (semctl(id, 0, IPC_STAT, &semds) == -1)
1922 -#endif
1923                 return -1;
1924             getinfo = (cmd == GETALL);
1925 -#ifdef __linux__       /* XXX Need metaconfig test */
1926 -           infosize = semds.buf->sem_nsems * sizeof(short);
1927 -#else
1928             infosize = semds.sem_nsems * sizeof(short);
1929 -#endif
1930                 /* "short" is technically wrong but much more portable
1931                    than guessing about u_?short(_t)? */
1932         }
1933 @@ -1429,12 +1406,7 @@ SV **sp;
1934  #endif
1935  #ifdef HAS_SEM
1936      case OP_SEMCTL:
1937 -#ifdef __linux__       /* XXX Need metaconfig test */
1938 -        unsemds.buf = (struct semid_ds *)a;
1939 -       ret = semctl(id, n, cmd, unsemds);
1940 -#else
1941         ret = semctl(id, n, cmd, (struct semid_ds *)a);
1942 -#endif
1943         break;
1944  #endif
1945  #ifdef HAS_SHM
1946 EOPATCH
1947     }
1948     # Incorrect prototype added as part of 8ac853655d9b7447, fixed as part of
1949     # commit dc45a647708b6c54, with at least one intermediate modification.
1950     # Correct prototype for gethostbyaddr has socklen_t second. Linux has
1951     # uint32_t first for getnetbyaddr.
1952     # Easiest just to remove, instead of attempting more complex patching.
1953     # Something similar may be needed on other platforms.
1954     edit_file('pp_sys.c', sub {
1955                   my $code = shift;
1956                   $code =~ s/^    struct hostent \*(?:PerlSock_)?gethostbyaddr\([^)]+\);$//m;
1957                   $code =~ s/^    struct netent \*getnetbyaddr\([^)]+\);$//m;
1958                   return $code;
1959               });
1960 }
1961
1962 if ($major < 10 and -f 'ext/IPC/SysV/SysV.xs') {
1963     edit_file('ext/IPC/SysV/SysV.xs', sub {
1964                   my $xs = shift;
1965                   my $fixed = <<'EOFIX';
1966
1967 #include <sys/types.h>
1968 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
1969 #ifndef HAS_SEM
1970 #   include <sys/ipc.h>
1971 #endif
1972 #   ifdef HAS_MSG
1973 #       include <sys/msg.h>
1974 #   endif
1975 #   ifdef HAS_SHM
1976 #       if defined(PERL_SCO) || defined(PERL_ISC)
1977 #           include <sys/sysmacros.h>   /* SHMLBA */
1978 #       endif
1979 #      include <sys/shm.h>
1980 #      ifndef HAS_SHMAT_PROTOTYPE
1981            extern Shmat_t shmat (int, char *, int);
1982 #      endif
1983 #      if defined(HAS_SYSCONF) && defined(_SC_PAGESIZE)
1984 #          undef  SHMLBA /* not static: determined at boot time */
1985 #          define SHMLBA sysconf(_SC_PAGESIZE)
1986 #      elif defined(HAS_GETPAGESIZE)
1987 #          undef  SHMLBA /* not static: determined at boot time */
1988 #          define SHMLBA getpagesize()
1989 #      endif
1990 #   endif
1991 #endif
1992 EOFIX
1993                   $xs =~ s!
1994 #include <sys/types\.h>
1995 .*
1996 (#ifdef newCONSTSUB|/\* Required)!$fixed$1!ms;
1997                   return $xs;
1998               });
1999 }
2000
2001 if (-f 'ext/POSIX/Makefile.PL'
2002     && extract_from_file('ext/POSIX/Makefile.PL',
2003                          qr/Explicitly avoid including/)) {
2004     # commit 6695a346c41138df, which effectively reverts 170888cff5e2ffb7
2005
2006     # PERL5LIB is populated by make_ext.pl with paths to the modules we need
2007     # to run, don't override this with "../../lib" since that may not have
2008     # been populated yet in a parallel build.
2009     apply_commit('6695a346c41138df');
2010 }
2011
2012 # Parallel build for miniperl is safe
2013 system "make $j miniperl </dev/null";
2014
2015 my $expected = $target =~ /^test/ ? 't/perl'
2016     : $target eq 'Fcntl' ? "lib/auto/Fcntl/Fcntl.$Config{so}"
2017     : $target;
2018 my $real_target = $target eq 'Fcntl' ? $expected : $target;
2019
2020 if ($target ne 'miniperl') {
2021     # Nearly all parallel build issues fixed by 5.10.0. Untrustworthy before that.
2022     $j = '' if $major < 10;
2023
2024     if ($real_target eq 'test_prep') {
2025         if ($major < 8) {
2026             # test-prep was added in 5.004_01, 3e3baf6d63945cb6.
2027             # renamed to test_prep in 2001 in 5fe84fd29acaf55c.
2028             # earlier than that, just make test. It will be fast enough.
2029             $real_target = extract_from_file('Makefile.SH',
2030                                              qr/^(test[-_]prep):/,
2031                                              'test');
2032         }
2033     }
2034
2035     system "make $j $real_target </dev/null";
2036 }
2037
2038 my $missing_target = $expected =~ /perl$/ ? !-x $expected : !-r $expected;
2039
2040 if ($options{'test-build'}) {
2041     report_and_exit($missing_target, 'could build', 'could not build',
2042                     $real_target);
2043 } elsif ($missing_target) {
2044     skip("could not build $real_target");
2045 }
2046
2047 match_and_exit($real_target) if $match;
2048
2049 if (defined $options{'one-liner'}) {
2050     my $exe = $target =~ /^(?:perl$|test)/ ? 'perl' : 'miniperl';
2051     unshift @ARGV, "./$exe", '-Ilib', '-e', $options{'one-liner'};
2052 }
2053
2054 # This is what we came here to run:
2055
2056 if (exists $Config{ldlibpthname}) {
2057     require Cwd;
2058     my $varname = $Config{ldlibpthname};
2059     my $cwd = Cwd::getcwd();
2060     if (defined $ENV{$varname}) {
2061         $ENV{$varname} = $cwd . $Config{path_sep} . $ENV{$varname};
2062     } else {
2063         $ENV{$varname} = $cwd;
2064     }
2065 }
2066
2067 my $ret = system @ARGV;
2068
2069 report_and_exit($ret, 'zero exit from', 'non-zero exit from', "@ARGV");
2070
2071 # Local variables:
2072 # cperl-indent-level: 4
2073 # indent-tabs-mode: nil
2074 # End:
2075 #
2076 # ex: set ts=8 sts=4 sw=4 et: