This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Cloning a format whose outside has been undefined
[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      ld => 'cc',
57      ($linux64 ? (libpth => \@paths) : ()),
58     );
59
60 unless(GetOptions(\%options,
61                   'target=s', 'make=s', 'jobs|j=i', 'expect-pass=i',
62                   'expect-fail' => sub { $options{'expect-pass'} = 0; },
63                   'clean!', 'one-liner|e=s', 'c', 'l', 'w', 'match=s',
64                   'no-match=s' => sub {
65                       $options{match} = $_[1];
66                       $options{'expect-pass'} = 0;
67                   },
68                   'force-manifest', 'force-regen', 'test-build', 'validate',
69                   'check-args', 'check-shebang!', 'usage|help|?', 'A=s@',
70                   'D=s@' => sub {
71                       my (undef, $val) = @_;
72                       if ($val =~ /\A([^=]+)=(.*)/s) {
73                           $defines{$1} = length $2 ? $2 : "\0";
74                       } else {
75                           $defines{$val} = '';
76                       }
77                   },
78                   'U=s@' => sub {
79                       $defines{$_[1]} = undef;
80                   },
81                  )) {
82     pod2usage(exitval => 255, verbose => 1);
83 }
84
85 my ($target, $j, $match) = @options{qw(target jobs match)};
86
87 @ARGV = ('sh', '-c', 'cd t && ./perl TEST base/*.t')
88     if $options{validate} && !@ARGV;
89
90 pod2usage(exitval => 0, verbose => 2) if $options{usage};
91 pod2usage(exitval => 255, verbose => 1)
92     unless @ARGV || $match || $options{'test-build'} || defined $options{'one-liner'};
93 pod2usage(exitval => 255, verbose => 1)
94     if !$options{'one-liner'} && ($options{l} || $options{w});
95
96 check_shebang($ARGV[0])
97     if $options{'check-shebang'} && @ARGV && !$options{match};
98
99 exit 0 if $options{'check-args'};
100
101 =head1 NAME
102
103 bisect.pl - use git bisect to pinpoint changes
104
105 =head1 SYNOPSIS
106
107     # When did this become an error?
108     .../Porting/bisect.pl -e 'my $a := 2;'
109     # When did this stop being an error?
110     .../Porting/bisect.pl --expect-fail -e '1 // 2'
111     # When did this stop matching?
112     .../Porting/bisect.pl --match '\b(?:PL_)hash_seed_set\b'
113     # When did this start matching?
114     .../Porting/bisect.pl --expect-fail --match '\buseithreads\b'
115     # When did this test program stop working?
116     .../Porting/bisect.pl -- ./perl -Ilib ../test_prog.pl
117     # When did this test start failing?
118     .../Porting/bisect.pl -- ./perl -Ilib t/TEST op/sort.t
119     # When did this first become valid syntax?
120     .../Porting/bisect.pl --target=miniperl --end=v5.10.0 \
121          --expect-fail -e 'my $a := 2;'
122     # What was the last revision to build with these options?
123     .../Porting/bisect.pl --test-build -Dd_dosuid
124
125 =head1 DESCRIPTION
126
127 Together F<bisect.pl> and F<bisect-runner.pl> attempt to automate the use
128 of C<git bisect> as much as possible. With one command (and no other files)
129 it's easy to find out
130
131 =over 4
132
133 =item *
134
135 Which commit caused this example code to break?
136
137 =item *
138
139 Which commit caused this example code to start working?
140
141 =item *
142
143 Which commit added the first file to match this regex?
144
145 =item *
146
147 Which commit removed the last file to match this regex?
148
149 =back
150
151 usually without needing to know which versions of perl to use as start and
152 end revisions.
153
154 By default F<bisect.pl> will process all options, then use the rest of the
155 command line as arguments to list C<system> to run a test case. By default,
156 the test case should pass (exit with 0) on earlier perls, and fail (exit
157 non-zero) on I<blead> (note that running most of perl's test files directly
158 won't do this, you'll need to run them through a harness to get the proper
159 error code). F<bisect.pl> will use F<bisect-runner.pl> to find the earliest
160 stable perl version on which the test case passes, check that it fails on
161 blead, and then use F<bisect-runner.pl> with C<git bisect run> to find the
162 commit which caused the failure.
163
164 Because the test case is the complete argument to C<system>, it is easy to
165 run something other than the F<perl> built, if necessary. If you need to run
166 the perl built, you'll probably need to invoke it as C<./perl -Ilib ...>
167
168 You need a clean checkout to run a bisect, and you can't use the checkout
169 which contains F<Porting/bisect.pl> (because C<git bisect>) will check out
170 a revision before F<Porting/bisect-runner.pl> was added, which
171 C<git bisect run> needs). If your working checkout is called F<perl>, the
172 simplest solution is to make a local clone, and run from that. I<i.e.>:
173
174     cd ..
175     git clone perl perl2
176     cd perl2
177     ../perl/Porting/bisect.pl ...
178
179 By default, F<bisect-runner.pl> will automatically disable the build of
180 L<DB_File> for commits earlier than ccb44e3bf3be2c30, as it's not practical
181 to patch DB_File 1.70 and earlier to build with current Berkeley DB headers.
182 (ccb44e3bf3be2c30 was in September 1999, between 5.005_62 and 5.005_63.)
183 If your F<db.h> is old enough you can override this with C<-Unoextensions>.
184
185 =head1 OPTIONS
186
187 =over 4
188
189 =item *
190
191 --start I<commit-ish>
192
193 Earliest revision to test, as a I<commit-ish> (a tag, commit or anything
194 else C<git> understands as a revision). If not specified, F<bisect.pl> will
195 search stable perl releases until it finds one where the test case passes.
196 The default is to search from 5.002 to 5.14.0. If F<bisect.pl> detects that
197 the checkout is on a case insensitive file system, it will search from
198 5.005 to 5.14.0
199
200 =item *
201
202 --end I<commit-ish>
203
204 Most recent revision to test, as a I<commit-ish>. If not specified, defaults
205 to I<blead>.
206
207 =item *
208
209 --target I<target>
210
211 F<Makefile> target (or equivalent) needed, to run the test case. If specified,
212 this should be one of
213
214 =over 4
215
216 =item *
217
218 I<config.sh>
219
220 Just run F<./Configure>
221
222 =item *
223
224 I<config.h>
225
226 Run the various F<*.SH> files to generate F<Makefile>, F<config.h>, I<etc>.
227
228 =item *
229
230 I<miniperl>
231
232 Build F<miniperl>.
233
234 =item *
235
236 I<lib/Config.pm>
237
238 Use F<miniperl> to build F<lib/Config.pm>
239
240 =item *
241
242 I<Fcntl>
243
244 Build F<lib/auto/Fcntl/Fnctl.so> (strictly, C<.$Config{so}>). As L<Fcntl>
245 is simple XS module present since 5.000, this provides a fast test of
246 whether XS modules can be built. Note, XS modules are built by F<miniperl>,
247 hence this target will not build F<perl>.
248
249 =item *
250
251 I<perl>
252
253 Build F<perl>. This also builds pure-Perl modules in F<cpan>, F<dist> and
254 F<ext>. XS modules (such as L<Fcntl>) are not built.
255
256 =item *
257
258 I<test_prep>
259
260 Build everything needed to run the tests. This is the default if we're
261 running test code, but is time consuming, as it means building all
262 XS modules. For older F<Makefile>s, the previous name of C<test-prep>
263 is automatically substituted. For very old F<Makefile>s, C<make test> is
264 run, as there is no target provided to just get things ready, and for 5.004
265 and earlier the tests run very quickly.
266
267 =back
268
269 =item *
270
271 --one-liner 'code to run'
272
273 =item *
274
275 -e 'code to run'
276
277 Example code to run, just like you'd use with C<perl -e>.
278
279 This prepends C<./perl -Ilib -e 'code to run'> to the test case given,
280 or F<./miniperl> if I<target> is C<miniperl>.
281
282 (Usually you'll use C<-e> instead of providing a test case in the
283 non-option arguments to F<bisect.pl>)
284
285 C<-E> intentionally isn't supported, as it's an error in 5.8.0 and earlier,
286 which interferes with detecting errors in the example code itself.
287
288 =item *
289
290 -c
291
292 Add C<-c> to the command line, to cause perl to exit after syntax checking.
293
294 =item *
295
296 -l
297
298 Add C<-l> to the command line with C<-e>
299
300 This will automatically append a newline to every output line of your testcase.
301 Note that you can't specify an argument to F<perl>'s C<-l> with this, as it's
302 not feasible to emulate F<perl>'s somewhat quirky switch parsing with
303 L<Getopt::Long>. If you need the full flexibility of C<-l>, you need to write
304 a full test case, instead of using C<bisect.pl>'s C<-e> shortcut.
305
306 =item *
307
308 -w
309
310 Add C<-w> to the command line with C<-e>
311
312 It's not valid to pass C<-c>,  C<-l> or C<-w> to C<bisect.pl> unless you are
313 also using C<-e>
314
315 =item *
316
317 --expect-fail
318
319 The test case should fail for the I<start> revision, and pass for the I<end>
320 revision. The bisect run will find the first commit where it passes.
321
322 =item *
323
324 -Dnoextensions=Encode
325
326 =item *
327
328 -Uusedevel
329
330 =item *
331
332 -Accflags=-DNO_MATHOMS
333
334 Arguments to pass to F<Configure>. Repeated C<-A> arguments are passed
335 through as is. C<-D> and C<-U> are processed in order, and override
336 previous settings for the same parameter. F<bisect-runner.pl> emulates
337 C<-Dnoextensions> when F<Configure> itself does not provide it, as it's
338 often very useful to be able to disable some XS extensions.
339
340 =item *
341
342 --make I<make-prog>
343
344 The C<make> command to use. If this not set, F<make> is used. If this is
345 set, it also adds a C<-Dmake=...> else some recursive make invocations
346 in extensions may fail. Typically one would use this as C<--make gmake>
347 to use F<gmake> in place of the system F<make>.
348
349 =item *
350
351 --jobs I<jobs>
352
353 =item *
354
355 -j I<jobs>
356
357 Number of C<make> jobs to run in parallel. If F</proc/cpuinfo> exists and
358 can be parsed, or F</sbin/sysctl> exists and reports C<hw.ncpu>, or
359 F</usr/bin/getconf> exists and reports C<_NPROCESSORS_ONLN> defaults to 1 +
360 I<number of CPUs>. Otherwise defaults to 2.
361
362 =item *
363
364 --match pattern
365
366 =item *
367
368 --no-match pattern
369
370 Instead of running a test program to determine I<pass> or I<fail>,
371 C<--match> will pass if the given regex matches, and hence search for the
372 commit that removes the last matching file. C<--no-match> inverts the test,
373 to search for the first commit that adds files that match.
374
375 The remaining command line arguments are treated as glob patterns for files
376 to match against. If none are specified, then they default as follows:
377
378 =over 4
379
380 =item *
381
382 If no I<target> is specified, the match is against all files in the
383 repository (which is fast).
384
385 =item *
386
387 If a I<target> is specified, that target is built, and the match is against
388 only the built files.
389
390 =back
391
392 Treating the command line arguments as glob patterns should not cause
393 problems, as the perl distribution has never shipped or built files with
394 names that contain characters which are globbing metacharacters.
395
396 Anything which is not a readable file is ignored, instead of generating an
397 error. (If you want an error, run C<grep> or C<ack> as a test case). This
398 permits one to easily search in a file that changed its name. For example:
399
400     .../Porting/bisect.pl --match 'Pod.*Functions' 'pod/buildtoc*'
401
402 C<--no-match ...> is implemented as C<--expect-fail --match ...>
403
404 =item *
405
406 --test-build
407
408 Test that the build completes, without running any test case.
409
410 By default, if the build for the desired I<target> fails to complete,
411 F<bisect-runner.pl> reports a I<skip> back to C<git bisect>, the assumption
412 being that one wants to find a commit which changed state "builds && passes"
413 to "builds && fails". If instead one is interested in which commit broke the
414 build (possibly for particular F<Configure> options), use I<--test-build>
415 to treat a build failure as a failure, not a "skip".
416
417 Often this option isn't as useful as it first seems, because I<any> build
418 failure will be reported to C<git bisect> as a failure, not just the failure
419 that you're interested in. Generally, to debug a particular problem, it's
420 more useful to use a I<target> that builds properly at the point of interest,
421 and then a test case that runs C<make>. For example:
422
423     .../Porting/bisect.pl --start=perl-5.000 --end=perl-5.002 \
424         --expect-fail --force-manifest --target=miniperl make perl
425
426 will find the first revision capable of building L<DynaLoader> and then
427 F<perl>, without becoming confused by revisions where F<miniperl> won't
428 even link.
429
430 =item *
431
432 --force-manifest
433
434 By default, a build will "skip" if any files listed in F<MANIFEST> are not
435 present. Usually this is useful, as it avoids false-failures. However, there
436 are some long ranges of commits where listed files are missing, which can
437 cause a bisect to abort because all that remain are skipped revisions.
438
439 In these cases, particularly if the test case uses F<miniperl> and no modules,
440 it may be more useful to force the build to continue, even if files
441 F<MANIFEST> are missing.
442
443 =item *
444
445 --force-regen
446
447 Run C<make regen_headers> before building F<miniperl>. This may fix a build
448 that otherwise would skip because the generated headers at that revision
449 are stale. It's not the default because it conceals this error in the true
450 state of such revisions.
451
452 =item *
453
454 --expect-pass [0|1]
455
456 C<--expect-pass=0> is equivalent to C<--expect-fail>. I<1> is the default.
457
458 =item *
459
460 --no-clean
461
462 Tell F<bisect-runner.pl> not to clean up after the build. This allows one
463 to use F<bisect-runner.pl> to build the current particular perl revision for
464 interactive testing, or for debugging F<bisect-runner.pl>.
465
466 Passing this to F<bisect.pl> will likely cause the bisect to fail badly.
467
468 =item *
469
470 --validate
471
472 Test that all stable revisions can be built. By default, attempts to build
473 I<blead>, I<v5.14.0> .. I<perl-5.002> (or I<perl5.005> on a case insensitive
474 file system). Stops at the first failure, without
475 cleaning the checkout. Use I<--start> to specify the earliest revision to
476 test, I<--end> to specify the most recent. Useful for validating a new
477 OS/CPU/compiler combination. For example
478
479     ../perl/Porting/bisect.pl --validate -le 'print "Hello from $]"'
480
481 If no testcase is specified, the default is to use F<t/TEST> to run
482 F<t/base/*.t>
483
484 =item *
485
486 --check-args
487
488 Validate the options and arguments, and exit silently if they are valid.
489
490 =item *
491
492 --check-shebang
493
494 Validate that the test case isn't an executable file with a
495 C<#!/usr/bin/perl> line (or similar). As F<bisect-runner.pl> does B<not>
496 prepend C<./perl> to the test case, a I<#!> line specifying an external
497 F<perl> binary will cause the test case to always run with I<that> F<perl>,
498 not the F<perl> built by the bisect runner. Likely this is not what you
499 wanted. If your test case is actually a wrapper script to run other
500 commands, you should run it with an explicit interpreter, to be clear. For
501 example, instead of C<../perl/Porting/bisect.pl ~/test/testcase.pl> you'd
502 run C<../perl/Porting/bisect.pl /usr/bin/perl ~/test/testcase.pl>
503
504 =item *
505
506 --usage
507
508 =item *
509
510 --help
511
512 =item *
513
514 -?
515
516 Display the usage information and exit.
517
518 =back
519
520 =cut
521
522 die "$0: Can't build $target" if defined $target && !grep {@targets} $target;
523
524 unless (exists $defines{cc}) {
525     # If it fails, the heuristic of 63f9ec3008baf7d6 is noisy, and hence
526     # confusing.
527     # FIXME - really it should be replaced with a proper test of
528     # "can we build something?" and a helpful diagnostic if we can't.
529     # For now, simply move it here.
530     $defines{cc} = (`ccache -V`, $?) ? 'cc' : 'ccache cc';
531 }
532
533 $j = "-j$j" if $j =~ /\A\d+\z/;
534
535 if (exists $options{make}) {
536     if (!exists $defines{make}) {
537         $defines{make} = $options{make};
538     }
539 } else {
540     $options{make} = 'make';
541 }
542
543 # Sadly, however hard we try, I don't think that it will be possible to build
544 # modules in ext/ on x86_64 Linux before commit e1666bf5602ae794 on 1999/12/29,
545 # which updated to MakeMaker 3.7, which changed from using a hard coded ld
546 # in the Makefile to $(LD). On x86_64 Linux the "linker" is gcc.
547
548 sub open_or_die {
549     my $file = shift;
550     my $mode = @_ ? shift : '<';
551     open my $fh, $mode, $file or croak("Can't open $file: $!");
552     ${*$fh{SCALAR}} = $file;
553     return $fh;
554 }
555
556 sub close_or_die {
557     my $fh = shift;
558     return if close $fh;
559     croak("Can't close: $!") unless ref $fh eq 'GLOB';
560     croak("Can't close ${*$fh{SCALAR}}: $!");
561 }
562
563 sub extract_from_file {
564     my ($file, $rx, $default) = @_;
565     my $fh = open_or_die($file);
566     while (<$fh>) {
567         my @got = $_ =~ $rx;
568         return wantarray ? @got : $got[0]
569             if @got;
570     }
571     return $default if defined $default;
572     return;
573 }
574
575 sub edit_file {
576     my ($file, $munger) = @_;
577     local $/;
578     my $fh = open_or_die($file);
579     my $orig = <$fh>;
580     die "Can't read $file: $!" unless defined $orig && close $fh;
581     my $new = $munger->($orig);
582     return if $new eq $orig;
583     $fh = open_or_die($file, '>');
584     print $fh $new or die "Can't print to $file: $!";
585     close_or_die($fh);
586 }
587
588 # AIX supplies a pre-historic patch program, which certainly predates Linux
589 # and is probably older than NT. It can't cope with unified diffs. Meanwhile,
590 # it's hard enough to get git diff to output context diffs, let alone git show,
591 # and nearly all the patches embedded here are unified. So it seems that the
592 # path of least resistance is to convert unified diffs to context diffs:
593
594 sub process_hunk {
595     my ($from_out, $to_out, $has_from, $has_to, $delete, $add) = @_;
596     ++$$has_from if $delete;
597     ++$$has_to if $add;
598
599     if ($delete && $add) {
600         $$from_out .= "! $_\n" foreach @$delete;
601         $$to_out .= "! $_\n" foreach @$add;
602     } elsif ($delete) {
603         $$from_out .= "- $_\n" foreach @$delete;
604     } elsif ($add) {
605          $$to_out .= "+ $_\n" foreach @$add;
606     }
607 }
608
609 # This isn't quite general purpose, as it can't cope with
610 # '\ No newline at end of file'
611 sub ud2cd {
612     my $diff_in = shift;
613     my $diff_out = '';
614
615     # Stuff before the diff
616     while ($diff_in =~ s/\A(?!\*\*\* )(?!--- )([^\n]*\n?)//ms && length $1) {
617         $diff_out .= $1;
618     }
619
620     if (!length $diff_in) {
621         die "That didn't seem to be a diff";
622     }
623
624     if ($diff_in =~ /\A\*\*\* /ms) {
625         warn "Seems to be a context diff already\n";
626         return $diff_out . $diff_in;
627     }
628
629     # Loop for files
630  FILE: while (1) {
631         if ($diff_in =~ s/\A((?:diff |index )[^\n]+\n)//ms) {
632             $diff_out .= $1;
633             next;
634         }
635         if ($diff_in !~ /\A--- /ms) {
636             # Stuff after the diff;
637             return $diff_out . $diff_in;
638         }
639         $diff_in =~ s/\A([^\n]+\n?)//ms;
640         my $line = $1;
641         die "Can't parse '$line'" unless $line =~ s/\A--- /*** /ms;
642         $diff_out .= $line;
643         $diff_in =~ s/\A([^\n]+\n?)//ms;
644         $line = $1;
645         die "Can't parse '$line'" unless $line =~ s/\A\+\+\+ /--- /ms;
646         $diff_out .= $line;
647
648         # Loop for hunks
649         while (1) {
650             next FILE
651                 unless $diff_in =~ s/\A\@\@ (-([0-9]+),([0-9]+) \+([0-9]+),([0-9]+)) \@\@[^\n]*\n?//;
652             my ($hunk, $from_start, $from_count, $to_start, $to_count)
653                 = ($1, $2, $3, $4, $5);
654             my $from_end = $from_start + $from_count - 1;
655             my $to_end = $to_start + $to_count - 1;
656             my ($from_out, $to_out, $has_from, $has_to, $add, $delete);
657             while (length $diff_in && ($from_count || $to_count)) {
658                 die "Confused in $hunk" unless $diff_in =~ s/\A([^\n]*)\n//ms;
659                 my $line = $1;
660                 $line = ' ' unless length $line;
661                 if ($line =~ /^ .*/) {
662                     process_hunk(\$from_out, \$to_out, \$has_from, \$has_to,
663                                  $delete, $add);
664                     undef $delete;
665                     undef $add;
666                     $from_out .= " $line\n";
667                     $to_out .= " $line\n";
668                     --$from_count;
669                     --$to_count;
670                 } elsif ($line =~ /^-(.*)/) {
671                     push @$delete, $1;
672                     --$from_count;
673                 } elsif ($line =~ /^\+(.*)/) {
674                     push @$add, $1;
675                     --$to_count;
676                 } else {
677                     die "Can't parse '$line' as part of hunk $hunk";
678                 }
679             }
680             process_hunk(\$from_out, \$to_out, \$has_from, \$has_to,
681                          $delete, $add);
682             die "No lines in hunk $hunk"
683                 unless length $from_out || length $to_out;
684             die "No changes in hunk $hunk"
685                 unless $has_from || $has_to;
686             $diff_out .= "***************\n";
687             $diff_out .= "*** $from_start,$from_end ****\n";
688             $diff_out .= $from_out if $has_from;
689             $diff_out .= "--- $to_start,$to_end ----\n";
690             $diff_out .= $to_out if $has_to;
691         }
692     }
693 }
694
695 {
696     my $use_context;
697
698     sub placate_patch_prog {
699         my $patch = shift;
700
701         if (!defined $use_context) {
702             my $version = `patch -v 2>&1`;
703             die "Can't run `patch -v`, \$?=$?, bailing out"
704                 unless defined $version;
705             if ($version =~ /Free Software Foundation/) {
706                 $use_context = 0;
707             } elsif ($version =~ /Header: patch\.c,v.*\blwall\b/) {
708                 # The system patch is older than Linux, and probably older than
709                 # Windows NT.
710                 $use_context = 1;
711             } else {
712                 # Don't know.
713                 $use_context = 0;
714             }
715         }
716
717         return $use_context ? ud2cd($patch) : $patch;
718     }
719 }
720
721 sub apply_patch {
722     my ($patch, $what, $files) = @_;
723     $what = 'patch' unless defined $what;
724     unless (defined $files) {
725         $patch =~ m!^--- a/(\S+)\n\+\+\+ b/\1!sm;
726         $files = " $1";
727     }
728     my $patch_to_use = placate_patch_prog($patch);
729     open my $fh, '|-', 'patch', '-p1' or die "Can't run patch: $!";
730     print $fh $patch_to_use;
731     return if close $fh;
732     print STDERR "Patch is <<'EOPATCH'\n${patch}EOPATCH\n";
733     print STDERR "\nConverted to a context diff <<'EOCONTEXT'\n${patch_to_use}EOCONTEXT\n"
734         if $patch_to_use ne $patch;
735     die "Can't $what$files: $?, $!";
736 }
737
738 sub apply_commit {
739     my ($commit, @files) = @_;
740     my $patch = `git show $commit @files`;
741     if (!defined $patch) {
742         die "Can't get commit $commit for @files: $?" if @files;
743         die "Can't get commit $commit: $?";
744     }
745     apply_patch($patch, "patch $commit", @files ? " for @files" : '');
746 }
747
748 sub revert_commit {
749     my ($commit, @files) = @_;
750     my $patch = `git show -R $commit @files`;
751     if (!defined $patch) {
752         die "Can't get revert commit $commit for @files: $?" if @files;
753         die "Can't get revert commit $commit: $?";
754     }
755     apply_patch($patch, "revert $commit", @files ? " for @files" : '');
756 }
757
758 sub checkout_file {
759     my ($file, $commit) = @_;
760     $commit ||= 'blead';
761     system "git show $commit:$file > $file </dev/null"
762         and die "Could not extract $file at revision $commit";
763 }
764
765 sub check_shebang {
766     my $file = shift;
767     return unless -e $file;
768     if (!-x $file) {
769         die "$file is not executable.
770 system($file, ...) is always going to fail.
771
772 Bailing out";
773     }
774     my $fh = open_or_die($file);
775     my $line = <$fh>;
776     return unless $line =~ m{\A#!(/\S+/perl\S*)\s};
777     die "$file will always be run by $1
778 It won't be tested by the ./perl we build.
779 If you intended to run it with that perl binary, please change your
780 test case to
781
782     $1 @ARGV
783
784 If you intended to test it with the ./perl we build, please change your
785 test case to
786
787     ./perl -Ilib @ARGV
788
789 [You may also need to add -- before ./perl to prevent that -Ilib as being
790 parsed as an argument to bisect.pl]
791
792 Bailing out";
793 }
794
795 sub clean {
796     if ($options{clean}) {
797         # Needed, because files that are build products in this checked out
798         # version might be in git in the next desired version.
799         system 'git clean -dxf </dev/null';
800         # Needed, because at some revisions the build alters checked out files.
801         # (eg pod/perlapi.pod). Also undoes any changes to makedepend.SH
802         system 'git reset --hard HEAD </dev/null';
803     }
804 }
805
806 sub skip {
807     my $reason = shift;
808     clean();
809     warn "skipping - $reason";
810     exit 125;
811 }
812
813 sub report_and_exit {
814     my ($ret, $pass, $fail, $desc) = @_;
815
816     clean();
817
818     my $got = ($options{'expect-pass'} ? !$ret : $ret) ? 'good' : 'bad';
819     if ($ret) {
820         print "$got - $fail $desc\n";
821     } else {
822         print "$got - $pass $desc\n";
823     }
824
825     exit($got eq 'bad');
826 }
827
828 sub match_and_exit {
829     my ($target, @globs) = @_;
830     my $matches = 0;
831     my $re = qr/$match/;
832     my @files;
833
834     if (@globs) {
835         require File::Glob;
836         foreach (sort map { File::Glob::bsd_glob($_)} @globs) {
837             if (!-f $_ || !-r _) {
838                 warn "Skipping matching '$_' as it is not a readable file\n";
839             } else {
840                 push @files, $_;
841             }
842         }
843     } else {
844         local $/ = "\0";
845         @files = defined $target ? `git ls-files -o -z`: `git ls-files -z`;
846         chomp @files;
847     }
848
849     foreach my $file (@files) {
850         my $fh = open_or_die($file);
851         while (<$fh>) {
852             if ($_ =~ $re) {
853                 ++$matches;
854                 if (tr/\t\r\n -~\200-\377//c) {
855                     print "Binary file $file matches\n";
856                 } else {
857                     $_ .= "\n" unless /\n\z/;
858                     print "$file: $_";
859                 }
860             }
861         }
862         close_or_die($fh);
863     }
864     report_and_exit(!$matches,
865                     $matches == 1 ? '1 match for' : "$matches matches for",
866                     'no matches for', $match);
867 }
868
869 # Not going to assume that system perl is yet new enough to have autodie
870 system 'git clean -dxf </dev/null' and die;
871
872 if (!defined $target) {
873     match_and_exit(undef, @ARGV) if $match;
874     $target = 'test_prep';
875 }
876
877 skip('no Configure - is this the //depot/perlext/Compiler branch?')
878     unless -f 'Configure';
879
880 my $case_insensitive;
881 {
882     my ($dev_C, $ino_C) = stat 'Configure';
883     die "Could not stat Configure: $!" unless defined $dev_C;
884     my ($dev_c, $ino_c) = stat 'configure';
885     ++$case_insensitive
886         if defined $dev_c && $dev_C == $dev_c && $ino_C == $ino_c;
887 }
888
889 # This changes to PERL_VERSION in 4d8076ea25903dcb in 1999
890 my $major
891     = extract_from_file('patchlevel.h',
892                         qr/^#define\s+(?:PERL_VERSION|PATCHLEVEL)\s+(\d+)\s/,
893                         0);
894
895 patch_Configure();
896 patch_hints();
897
898 # if Encode is not needed for the test, you can speed up the bisect by
899 # excluding it from the runs with -Dnoextensions=Encode
900 # ccache is an easy win. Remove it if it causes problems.
901 # Commit 1cfa4ec74d4933da adds ignore_versioned_solibs to Configure, and sets it
902 # to true in hints/linux.sh
903 # On dromedary, from that point on, Configure (by default) fails to find any
904 # libraries, because it scans /usr/local/lib /lib /usr/lib, which only contain
905 # versioned libraries. Without -lm, the build fails.
906 # Telling /usr/local/lib64 /lib64 /usr/lib64 works from that commit onwards,
907 # until commit faae14e6e968e1c0 adds it to the hints.
908 # However, prior to 1cfa4ec74d4933da telling Configure the truth doesn't work,
909 # because it will spot versioned libraries, pass them to the compiler, and then
910 # bail out pretty early on. Configure won't let us override libswanted, but it
911 # will let us override the entire libs list.
912
913 unless (extract_from_file('Configure', 'ignore_versioned_solibs')) {
914     # Before 1cfa4ec74d4933da, so force the libs list.
915
916     my @libs;
917     # This is the current libswanted list from Configure, less the libs removed
918     # by current hints/linux.sh
919     foreach my $lib (qw(sfio socket inet nsl nm ndbm gdbm dbm db malloc dl dld
920                         ld sun m crypt sec util c cposix posix ucb BSD)) {
921         foreach my $dir (@paths) {
922             next unless -f "$dir/lib$lib.so";
923             push @libs, "-l$lib";
924             last;
925         }
926     }
927     $defines{libs} = \@libs unless exists $defines{libs};
928 }
929
930 $defines{usenm} = undef
931     if $major < 2 && !exists $defines{usenm};
932
933 my ($missing, $created_dirs);
934 ($missing, $created_dirs) = force_manifest()
935     if $options{'force-manifest'};
936
937 my @ARGS = '-dEs';
938 foreach my $key (sort keys %defines) {
939     my $val = $defines{$key};
940     if (ref $val) {
941         push @ARGS, "-D$key=@$val";
942     } elsif (!defined $val) {
943         push @ARGS, "-U$key";
944     } elsif (!length $val) {
945         push @ARGS, "-D$key";
946     } else {
947         $val = "" if $val eq "\0";
948         push @ARGS, "-D$key=$val";
949     }
950 }
951 push @ARGS, map {"-A$_"} @{$options{A}};
952
953 # </dev/null because it seems that some earlier versions of Configure can
954 # call commands in a way that now has them reading from stdin (and hanging)
955 my $pid = fork;
956 die "Can't fork: $!" unless defined $pid;
957 if (!$pid) {
958     open STDIN, '<', '/dev/null';
959     # If a file in MANIFEST is missing, Configure asks if you want to
960     # continue (the default being 'n'). With stdin closed or /dev/null,
961     # it exits immediately and the check for config.sh below will skip.
962     exec './Configure', @ARGS;
963     die "Failed to start Configure: $!";
964 }
965 waitpid $pid, 0
966     or die "wait for Configure, pid $pid failed: $!";
967
968 patch_SH();
969
970 if (-f 'config.sh') {
971     # Emulate noextensions if Configure doesn't support it.
972     fake_noextensions()
973         if $major < 10 && $defines{noextensions};
974     system './Configure -S </dev/null' and die;
975 }
976
977 if ($target =~ /config\.s?h/) {
978     match_and_exit($target, @ARGV) if $match && -f $target;
979     report_and_exit(!-f $target, 'could build', 'could not build', $target)
980         if $options{'test-build'};
981
982     skip("could not build $target") unless -f $target;
983
984     my $ret = system @ARGV;
985     report_and_exit($ret, 'zero exit from', 'non-zero exit from', "@ARGV");
986 } elsif (!-f 'config.sh') {
987     # Skip if something went wrong with Configure
988
989     skip('could not build config.sh');
990 }
991
992 force_manifest_cleanup($missing, $created_dirs)
993         if $missing;
994
995 if($options{'force-regen'}
996    && extract_from_file('Makefile', qr/\bregen_headers\b/)) {
997     # regen_headers was added in e50aee73b3d4c555, patch.1m for perl5.001
998     # It's not worth faking it for earlier revisions.
999     system "make regen_headers </dev/null"
1000         and die;
1001 }
1002
1003 patch_C();
1004 patch_ext();
1005
1006 # Parallel build for miniperl is safe
1007 system "$options{make} $j miniperl </dev/null";
1008
1009 # This is the file we expect make to create
1010 my $expected_file = $target =~ /^test/ ? 't/perl'
1011     : $target eq 'Fcntl' ? "lib/auto/Fcntl/Fcntl.$Config{so}"
1012     : $target;
1013 # This is the target we tell make to build in order to get $expected_file
1014 my $real_target = $target eq 'Fcntl' ? $expected_file : $target;
1015
1016 if ($target ne 'miniperl') {
1017     # Nearly all parallel build issues fixed by 5.10.0. Untrustworthy before that.
1018     $j = '' if $major < 10;
1019
1020     if ($real_target eq 'test_prep') {
1021         if ($major < 8) {
1022             # test-prep was added in 5.004_01, 3e3baf6d63945cb6.
1023             # renamed to test_prep in 2001 in 5fe84fd29acaf55c.
1024             # earlier than that, just make test. It will be fast enough.
1025             $real_target = extract_from_file('Makefile.SH',
1026                                              qr/^(test[-_]prep):/,
1027                                              'test');
1028         }
1029     }
1030
1031     system "$options{make} $j $real_target </dev/null";
1032 }
1033
1034 my $expected_file_found = $expected_file =~ /perl$/
1035     ? -x $expected_file : -r $expected_file;
1036
1037 if ($expected_file_found && $expected_file eq 't/perl') {
1038     # Check that it isn't actually pointing to ../miniperl, which will happen
1039     # if the sanity check ./miniperl -Ilib -MExporter -e '<?>' fails, and
1040     # Makefile tries to run minitest.
1041
1042     # Of course, helpfully sometimes it's called ../perl, other times .././perl
1043     # and who knows if that list is exhaustive...
1044     my ($dev0, $ino0) = stat 't/perl';
1045     my ($dev1, $ino1) = stat 'perl';
1046     unless (defined $dev0 && defined $dev1 && $dev0 == $dev1 && $ino0 == $ino1) {
1047         undef $expected_file_found;
1048         my $link = readlink $expected_file;
1049         warn "'t/perl' => '$link', not 'perl'";
1050         die "Could not realink t/perl: $!" unless defined $link;
1051     }
1052 }
1053
1054 if ($options{'test-build'}) {
1055     report_and_exit(!$expected_file_found, 'could build', 'could not build',
1056                     $real_target);
1057 } elsif (!$expected_file_found) {
1058     skip("could not build $real_target");
1059 }
1060
1061 match_and_exit($real_target, @ARGV) if $match;
1062
1063 if (defined $options{'one-liner'}) {
1064     my $exe = $target =~ /^(?:perl$|test)/ ? 'perl' : 'miniperl';
1065     unshift @ARGV, '-e', $options{'one-liner'};
1066     foreach (qw(c l w)) {
1067         unshift @ARGV, "-$_" if $options{$_};
1068     }
1069     unshift @ARGV, "./$exe", '-Ilib';
1070 }
1071
1072 # This is what we came here to run:
1073
1074 if (exists $Config{ldlibpthname}) {
1075     require Cwd;
1076     my $varname = $Config{ldlibpthname};
1077     my $cwd = Cwd::getcwd();
1078     if (defined $ENV{$varname}) {
1079         $ENV{$varname} = $cwd . $Config{path_sep} . $ENV{$varname};
1080     } else {
1081         $ENV{$varname} = $cwd;
1082     }
1083 }
1084
1085 my $ret = system @ARGV;
1086
1087 report_and_exit($ret, 'zero exit from', 'non-zero exit from', "@ARGV");
1088
1089 ############################################################################
1090 #
1091 # Patching, editing and faking routines only below here.
1092 #
1093 ############################################################################
1094
1095 sub fake_noextensions {
1096     edit_file('config.sh', sub {
1097                   my @lines = split /\n/, shift;
1098                   my @ext = split /\s+/, $defines{noextensions};
1099                   foreach (@lines) {
1100                       next unless /^extensions=/ || /^dynamic_ext/;
1101                       foreach my $ext (@ext) {
1102                           s/\b$ext( )?\b/$1/;
1103                       }
1104                   }
1105                   return join "\n", @lines;
1106               });
1107 }
1108
1109 sub force_manifest {
1110     my (@missing, @created_dirs);
1111     my $fh = open_or_die('MANIFEST');
1112     while (<$fh>) {
1113         next unless /^(\S+)/;
1114         # -d is special case needed (at least) between 27332437a2ed1941 and
1115         # bf3d9ec563d25054^ inclusive, as manifest contains ext/Thread/Thread
1116         push @missing, $1
1117             unless -f $1 || -d $1;
1118     }
1119     close_or_die($fh);
1120
1121     foreach my $pathname (@missing) {
1122         my @parts = split '/', $pathname;
1123         my $leaf = pop @parts;
1124         my $path = '.';
1125         while (@parts) {
1126             $path .= '/' . shift @parts;
1127             next if -d $path;
1128             mkdir $path, 0700 or die "Can't create $path: $!";
1129             unshift @created_dirs, $path;
1130         }
1131         $fh = open_or_die($pathname, '>');
1132         close_or_die($fh);
1133         chmod 0, $pathname or die "Can't chmod 0 $pathname: $!";
1134     }
1135     return \@missing, \@created_dirs;
1136 }
1137
1138 sub force_manifest_cleanup {
1139     my ($missing, $created_dirs) = @_;
1140     # This is probably way too paranoid:
1141     my @errors;
1142     require Fcntl;
1143     foreach my $file (@$missing) {
1144         my (undef, undef, $mode, undef, undef, undef, undef, $size)
1145             = stat $file;
1146         if (!defined $mode) {
1147             push @errors, "Added file $file has been deleted by Configure";
1148             next;
1149         }
1150         if (Fcntl::S_IMODE($mode) != 0) {
1151             push @errors,
1152                 sprintf 'Added file %s had mode changed by Configure to %03o',
1153                     $file, $mode;
1154         }
1155         if ($size != 0) {
1156             push @errors,
1157                 "Added file $file had sized changed by Configure to $size";
1158         }
1159         unlink $file or die "Can't unlink $file: $!";
1160     }
1161     foreach my $dir (@$created_dirs) {
1162         rmdir $dir or die "Can't rmdir $dir: $!";
1163     }
1164     skip("@errors")
1165         if @errors;
1166 }
1167
1168 sub patch_Configure {
1169     if ($major < 1) {
1170         if (extract_from_file('Configure',
1171                               qr/^\t\t\*=\*\) echo "\$1" >> \$optdef;;$/)) {
1172             # This is "        Spaces now allowed in -D command line options.",
1173             # part of commit ecfc54246c2a6f42
1174             apply_patch(<<'EOPATCH');
1175 diff --git a/Configure b/Configure
1176 index 3d3b38d..78ffe16 100755
1177 --- a/Configure
1178 +++ b/Configure
1179 @@ -652,7 +777,8 @@ while test $# -gt 0; do
1180                         echo "$me: use '-U symbol=', not '-D symbol='." >&2
1181                         echo "$me: ignoring -D $1" >&2
1182                         ;;
1183 -               *=*) echo "$1" >> $optdef;;
1184 +               *=*) echo "$1" | \
1185 +                               sed -e "s/'/'\"'\"'/g" -e "s/=\(.*\)/='\1'/" >> $optdef;;
1186                 *) echo "$1='define'" >> $optdef;;
1187                 esac
1188                 shift
1189 EOPATCH
1190         }
1191
1192         if (extract_from_file('Configure', qr/^if \$contains 'd_namlen' \$xinc\b/)) {
1193             # Configure's original simple "grep" for d_namlen falls foul of the
1194             # approach taken by the glibc headers:
1195             # #ifdef _DIRENT_HAVE_D_NAMLEN
1196             # # define _D_EXACT_NAMLEN(d) ((d)->d_namlen)
1197             #
1198             # where _DIRENT_HAVE_D_NAMLEN is not defined on Linux.
1199             # This is also part of commit ecfc54246c2a6f42
1200             apply_patch(<<'EOPATCH');
1201 diff --git a/Configure b/Configure
1202 index 3d3b38d..78ffe16 100755
1203 --- a/Configure
1204 +++ b/Configure
1205 @@ -3935,7 +4045,8 @@ $rm -f try.c
1206  
1207  : see if the directory entry stores field length
1208  echo " "
1209 -if $contains 'd_namlen' $xinc >/dev/null 2>&1; then
1210 +$cppstdin $cppflags $cppminus < "$xinc" > try.c
1211 +if $contains 'd_namlen' try.c >/dev/null 2>&1; then
1212         echo "Good, your directory entry keeps length information in d_namlen." >&4
1213         val="$define"
1214  else
1215 EOPATCH
1216         }
1217     }
1218
1219     if ($major < 2
1220         && !extract_from_file('Configure',
1221                               qr/Try to guess additional flags to pick up local libraries/)) {
1222         my $mips = extract_from_file('Configure',
1223                                      qr!(''\) if (?:\./)?mips; then)!);
1224         # This is part of perl-5.001n. It's needed, to add -L/usr/local/lib to
1225         # theld flags if libraries are found there. It shifts the code to set up
1226         # libpth earlier, and then adds the code to add libpth entries to
1227         # ldflags
1228         # mips was changed to ./mips in ecfc54246c2a6f42, perl5.000 patch.0g
1229         apply_patch(sprintf <<'EOPATCH', $mips);
1230 diff --git a/Configure b/Configure
1231 index 53649d5..0635a6e 100755
1232 --- a/Configure
1233 +++ b/Configure
1234 @@ -2749,6 +2749,52 @@ EOM
1235         ;;
1236  esac
1237  
1238 +: Set private lib path
1239 +case "$plibpth" in
1240 +'') if ./mips; then
1241 +               plibpth="$incpath/usr/lib /usr/local/lib /usr/ccs/lib"
1242 +       fi;;
1243 +esac
1244 +case "$libpth" in
1245 +' ') dlist='';;
1246 +'') dlist="$plibpth $glibpth";;
1247 +*) dlist="$libpth";;
1248 +esac
1249 +
1250 +: Now check and see which directories actually exist, avoiding duplicates
1251 +libpth=''
1252 +for xxx in $dlist
1253 +do
1254 +    if $test -d $xxx; then
1255 +               case " $libpth " in
1256 +               *" $xxx "*) ;;
1257 +               *) libpth="$libpth $xxx";;
1258 +               esac
1259 +    fi
1260 +done
1261 +$cat <<'EOM'
1262 +
1263 +Some systems have incompatible or broken versions of libraries.  Among
1264 +the directories listed in the question below, please remove any you
1265 +know not to be holding relevant libraries, and add any that are needed.
1266 +Say "none" for none.
1267 +
1268 +EOM
1269 +case "$libpth" in
1270 +'') dflt='none';;
1271 +*)
1272 +       set X $libpth
1273 +       shift
1274 +       dflt=${1+"$@"}
1275 +       ;;
1276 +esac
1277 +rp="Directories to use for library searches?"
1278 +. ./myread
1279 +case "$ans" in
1280 +none) libpth=' ';;
1281 +*) libpth="$ans";;
1282 +esac
1283 +
1284  : flags used in final linking phase
1285  case "$ldflags" in
1286  '') if ./venix; then
1287 @@ -2765,6 +2811,23 @@ case "$ldflags" in
1288         ;;
1289  *) dflt="$ldflags";;
1290  esac
1291 +
1292 +: Possible local library directories to search.
1293 +loclibpth="/usr/local/lib /opt/local/lib /usr/gnu/lib"
1294 +loclibpth="$loclibpth /opt/gnu/lib /usr/GNU/lib /opt/GNU/lib"
1295 +
1296 +: Try to guess additional flags to pick up local libraries.
1297 +for thislibdir in $libpth; do
1298 +       case " $loclibpth " in
1299 +       *" $thislibdir "*)
1300 +               case "$dflt " in 
1301 +               "-L$thislibdir ") ;;
1302 +               *)  dflt="$dflt -L$thislibdir" ;;
1303 +               esac
1304 +               ;;
1305 +       esac
1306 +done
1307 +
1308  echo " "
1309  rp="Any additional ld flags (NOT including libraries)?"
1310  . ./myread
1311 @@ -2828,52 +2891,6 @@ n) echo "OK, that should do.";;
1312  esac
1313  $rm -f try try.* core
1314  
1315 -: Set private lib path
1316 -case "$plibpth" in
1317 -%s
1318 -               plibpth="$incpath/usr/lib /usr/local/lib /usr/ccs/lib"
1319 -       fi;;
1320 -esac
1321 -case "$libpth" in
1322 -' ') dlist='';;
1323 -'') dlist="$plibpth $glibpth";;
1324 -*) dlist="$libpth";;
1325 -esac
1326 -
1327 -: Now check and see which directories actually exist, avoiding duplicates
1328 -libpth=''
1329 -for xxx in $dlist
1330 -do
1331 -    if $test -d $xxx; then
1332 -               case " $libpth " in
1333 -               *" $xxx "*) ;;
1334 -               *) libpth="$libpth $xxx";;
1335 -               esac
1336 -    fi
1337 -done
1338 -$cat <<'EOM'
1339 -
1340 -Some systems have incompatible or broken versions of libraries.  Among
1341 -the directories listed in the question below, please remove any you
1342 -know not to be holding relevant libraries, and add any that are needed.
1343 -Say "none" for none.
1344 -
1345 -EOM
1346 -case "$libpth" in
1347 -'') dflt='none';;
1348 -*)
1349 -       set X $libpth
1350 -       shift
1351 -       dflt=${1+"$@"}
1352 -       ;;
1353 -esac
1354 -rp="Directories to use for library searches?"
1355 -. ./myread
1356 -case "$ans" in
1357 -none) libpth=' ';;
1358 -*) libpth="$ans";;
1359 -esac
1360 -
1361  : compute shared library extension
1362  case "$so" in
1363  '')
1364 EOPATCH
1365     }
1366
1367     if ($major < 5 && extract_from_file('Configure',
1368                                         qr!if \$cc \$ccflags try\.c -o try >/dev/null 2>&1; then!)) {
1369         # Analogous to the more general fix of dfe9444ca7881e71
1370         # Without this flags such as -m64 may not be passed to this compile,
1371         # which results in a byteorder of '1234' instead of '12345678', which
1372         # can then cause crashes.
1373
1374         if (extract_from_file('Configure', qr/xxx_prompt=y/)) {
1375             # 8e07c86ebc651fe9 or later
1376             # ("This is my patch  patch.1n  for perl5.001.")
1377             apply_patch(<<'EOPATCH');
1378 diff --git a/Configure b/Configure
1379 index 62249dd..c5c384e 100755
1380 --- a/Configure
1381 +++ b/Configure
1382 @@ -8247,7 +8247,7 @@ main()
1383  }
1384  EOCP
1385         xxx_prompt=y
1386 -       if $cc $ccflags try.c -o try >/dev/null 2>&1 && ./try > /dev/null; then
1387 +       if $cc $ccflags $ldflags try.c -o try >/dev/null 2>&1 && ./try > /dev/null; then
1388                 dflt=`./try`
1389                 case "$dflt" in
1390                 [1-4][1-4][1-4][1-4]|12345678|87654321)
1391 EOPATCH
1392         } else {
1393             apply_patch(<<'EOPATCH');
1394 diff --git a/Configure b/Configure
1395 index 53649d5..f1cd64a 100755
1396 --- a/Configure
1397 +++ b/Configure
1398 @@ -6362,7 +6362,7 @@ main()
1399         printf("\n");
1400  }
1401  EOCP
1402 -       if $cc $ccflags try.c -o try >/dev/null 2>&1 ; then
1403 +       if $cc $ccflags $ldflags try.c -o try >/dev/null 2>&1 ; then
1404                 dflt=`./try`
1405                 case "$dflt" in
1406                 ????|????????) echo "(The test program ran ok.)";;
1407 EOPATCH
1408         }
1409     }
1410
1411     if ($major < 6 && !extract_from_file('Configure',
1412                                          qr!^\t-A\)$!)) {
1413         # This adds the -A option to Configure, which is incredibly useful
1414         # Effectively this is commits 02e93a22d20fc9a5, 5f83a3e9d818c3ad,
1415         # bde6b06b2c493fef, f7c3111703e46e0c and 2 lines of trailing whitespace
1416         # removed by 613d6c3e99b9decc, but applied at slightly different
1417         # locations to ensure a clean patch back to 5.000
1418         # Note, if considering patching to the intermediate revisions to fix
1419         # bugs in -A handling, f7c3111703e46e0c is from 2002, and hence
1420         # $major == 8
1421
1422         # To add to the fun, early patches add -K and -O options, and it's not
1423         # trivial to get patch to put the C<. ./posthint.sh> in the right place
1424         edit_file('Configure', sub {
1425                       my $code = shift;
1426                       $code =~ s/(optstr = ")([^"]+";\s*# getopt-style specification)/$1A:$2/
1427                           or die "Substitution failed";
1428                       $code =~ s!^(: who configured the system)!
1429 touch posthint.sh
1430 . ./posthint.sh
1431
1432 $1!ms
1433                           or die "Substitution failed";
1434                       return $code;
1435                   });
1436         apply_patch(<<'EOPATCH');
1437 diff --git a/Configure b/Configure
1438 index 4b55fa6..60c3c64 100755
1439 --- a/Configure
1440 +++ b/Configure
1441 @@ -1150,6 +1150,7 @@ set X `for arg in "$@"; do echo "X$arg"; done |
1442  eval "set $*"
1443  shift
1444  rm -f options.awk
1445 +rm -f posthint.sh
1446  
1447  : set up default values
1448  fastread=''
1449 @@ -1172,6 +1173,56 @@ while test $# -gt 0; do
1450         case "$1" in
1451         -d) shift; fastread=yes;;
1452         -e) shift; alldone=cont;;
1453 +       -A)
1454 +           shift
1455 +           xxx=''
1456 +           yyy="$1"
1457 +           zzz=''
1458 +           uuu=undef
1459 +           case "$yyy" in
1460 +            *=*) zzz=`echo "$yyy"|sed 's!=.*!!'`
1461 +                 case "$zzz" in
1462 +                 *:*) zzz='' ;;
1463 +                 *)   xxx=append
1464 +                      zzz=" "`echo "$yyy"|sed 's!^[^=]*=!!'`
1465 +                      yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
1466 +                 esac
1467 +                 ;;
1468 +            esac
1469 +            case "$xxx" in
1470 +            '')  case "$yyy" in
1471 +                 *:*) xxx=`echo "$yyy"|sed 's!:.*!!'`
1472 +                      yyy=`echo "$yyy"|sed 's!^[^:]*:!!'`
1473 +                      zzz=`echo "$yyy"|sed 's!^[^=]*=!!'`
1474 +                      yyy=`echo "$yyy"|sed 's!=.*!!'` ;;
1475 +                 *)   xxx=`echo "$yyy"|sed 's!:.*!!'`
1476 +                      yyy=`echo "$yyy"|sed 's!^[^:]*:!!'` ;;
1477 +                 esac
1478 +                 ;;
1479 +            esac
1480 +           case "$xxx" in
1481 +           append)
1482 +               echo "$yyy=\"\${$yyy}$zzz\""    >> posthint.sh ;;
1483 +           clear)
1484 +               echo "$yyy=''"                  >> posthint.sh ;;
1485 +           define)
1486 +               case "$zzz" in
1487 +               '') zzz=define ;;
1488 +               esac
1489 +               echo "$yyy='$zzz'"              >> posthint.sh ;;
1490 +           eval)
1491 +               echo "eval \"$yyy=$zzz\""       >> posthint.sh ;;
1492 +           prepend)
1493 +               echo "$yyy=\"$zzz\${$yyy}\""    >> posthint.sh ;;
1494 +           undef)
1495 +               case "$zzz" in
1496 +               '') zzz="$uuu" ;;
1497 +               esac
1498 +               echo "$yyy=$zzz"                >> posthint.sh ;;
1499 +            *)  echo "$me: unknown -A command '$xxx', ignoring -A $1" >&2 ;;
1500 +           esac
1501 +           shift
1502 +           ;;
1503         -f)
1504                 shift
1505                 cd ..
1506 EOPATCH
1507     }
1508
1509     if ($major < 8 && $^O eq 'aix') {
1510         edit_file('Configure', sub {
1511                       my $code = shift;
1512                       # Replicate commit a8c676c69574838b
1513                       # Whitespace allowed at the ends of /lib/syscalls.exp lines
1514                       # and half of commit c6912327ae30e6de
1515                       # AIX syscalls.exp scan: the syscall might be marked 32, 3264, or 64
1516                       $code =~ s{(\bsed\b.*\bsyscall)(?:\[0-9\]\*)?(\$.*/lib/syscalls\.exp)}
1517                                 {$1 . "[0-9]*[ \t]*" . $2}e;
1518                       return $code;
1519                   });
1520     }
1521
1522     if ($major < 8 && !extract_from_file('Configure',
1523                                          qr/^\t\tif test ! -t 0; then$/)) {
1524         # Before dfe9444ca7881e71, Configure would refuse to run if stdin was
1525         # not a tty. With that commit, the tty requirement was dropped for -de
1526         # and -dE
1527         # Commit aaeb8e512e8e9e14 dropped the tty requirement for -S
1528         # For those older versions, it's probably easiest if we simply remove
1529         # the sanity test.
1530         edit_file('Configure', sub {
1531                       my $code = shift;
1532                       $code =~ s/test ! -t 0/test Perl = rules/;
1533                       return $code;
1534                   });
1535     }
1536
1537     if ($major == 8 || $major == 9) {
1538         # Fix symbol detection to that of commit 373dfab3839ca168 if it's any
1539         # intermediate version 5129fff43c4fe08c or later, as the intermediate
1540         # versions don't work correctly on (at least) Sparc Linux.
1541         # 5129fff43c4fe08c adds the first mention of mistrustnm.
1542         # 373dfab3839ca168 removes the last mention of lc=""
1543         edit_file('Configure', sub {
1544                       my $code = shift;
1545                       return $code
1546                           if $code !~ /\btc="";/; # 373dfab3839ca168 or later
1547                       return $code
1548                           if $code !~ /\bmistrustnm\b/; # before 5129fff43c4fe08c
1549                       my $fixed = <<'EOC';
1550
1551 : is a C symbol defined?
1552 csym='tlook=$1;
1553 case "$3" in
1554 -v) tf=libc.tmp; tdc="";;
1555 -a) tf=libc.tmp; tdc="[]";;
1556 *) tlook="^$1\$"; tf=libc.list; tdc="()";;
1557 esac;
1558 tx=yes;
1559 case "$reuseval-$4" in
1560 true-) ;;
1561 true-*) tx=no; eval "tval=\$$4"; case "$tval" in "") tx=yes;; esac;;
1562 esac;
1563 case "$tx" in
1564 yes)
1565         tval=false;
1566         if $test "$runnm" = true; then
1567                 if $contains $tlook $tf >/dev/null 2>&1; then
1568                         tval=true;
1569                 elif $test "$mistrustnm" = compile -o "$mistrustnm" = run; then
1570                         echo "void *(*(p()))$tdc { extern void *$1$tdc; return &$1; } int main() { if(p()) return(0); else return(1); }"> try.c;
1571                         $cc -o try $optimize $ccflags $ldflags try.c >/dev/null 2>&1 $libs && tval=true;
1572                         $test "$mistrustnm" = run -a -x try && { $run ./try$_exe >/dev/null 2>&1 || tval=false; };
1573                         $rm -f try$_exe try.c core core.* try.core;
1574                 fi;
1575         else
1576                 echo "void *(*(p()))$tdc { extern void *$1$tdc; return &$1; } int main() { if(p()) return(0); else return(1); }"> try.c;
1577                 $cc -o try $optimize $ccflags $ldflags try.c $libs >/dev/null 2>&1 && tval=true;
1578                 $rm -f try$_exe try.c;
1579         fi;
1580         ;;
1581 *)
1582         case "$tval" in
1583         $define) tval=true;;
1584         *) tval=false;;
1585         esac;
1586         ;;
1587 esac;
1588 eval "$2=$tval"'
1589
1590 EOC
1591                       $code =~ s/\n: is a C symbol defined\?\n.*?\neval "\$2=\$tval"'\n\n/$fixed/sm
1592                           or die "substitution failed";
1593                       return $code;
1594                   });
1595     }
1596
1597     if ($major < 10
1598         && extract_from_file('Configure', qr/^set malloc\.h i_malloc$/)) {
1599         # This is commit 01d07975f7ef0e7d, trimmed, with $compile inlined as
1600         # prior to bd9b35c97ad661cc Configure had the malloc.h test before the
1601         # definition of $compile.
1602         apply_patch(<<'EOPATCH');
1603 diff --git a/Configure b/Configure
1604 index 3d2e8b9..6ce7766 100755
1605 --- a/Configure
1606 +++ b/Configure
1607 @@ -6743,5 +6743,22 @@ set d_dosuid
1608  
1609  : see if this is a malloc.h system
1610 -set malloc.h i_malloc
1611 -eval $inhdr
1612 +: we want a real compile instead of Inhdr because some systems have a
1613 +: malloc.h that just gives a compile error saying to use stdlib.h instead
1614 +echo " "
1615 +$cat >try.c <<EOCP
1616 +#include <stdlib.h>
1617 +#include <malloc.h>
1618 +int main () { return 0; }
1619 +EOCP
1620 +set try
1621 +if $cc $optimize $ccflags $ldflags -o try $* try.c $libs > /dev/null 2>&1; then
1622 +    echo "<malloc.h> found." >&4
1623 +    val="$define"
1624 +else
1625 +    echo "<malloc.h> NOT found." >&4
1626 +    val="$undef"
1627 +fi
1628 +$rm -f try.c try
1629 +set i_malloc
1630 +eval $setvar
1631  
1632 EOPATCH
1633     }
1634 }
1635
1636 sub patch_hints {
1637     if ($^O eq 'freebsd') {
1638         # There are rather too many version-specific FreeBSD hints fixes to
1639         # patch individually. Also, more than once the FreeBSD hints file has
1640         # been written in what turned out to be a rather non-future-proof style,
1641         # with case statements treating the most recent version as the
1642         # exception, instead of treating previous versions' behaviour explicitly
1643         # and changing the default to cater for the current behaviour. (As
1644         # strangely, future versions inherit the current behaviour.)
1645         checkout_file('hints/freebsd.sh');
1646     } elsif ($^O eq 'darwin') {
1647         if ($major < 8) {
1648             # We can't build on darwin without some of the data in the hints
1649             # file. Probably less surprising to use the earliest version of
1650             # hints/darwin.sh and then edit in place just below, than use
1651             # blead's version, as that would create a discontinuity at
1652             # f556e5b971932902 - before it, hints bugs would be "fixed", after
1653             # it they'd resurface. This way, we should give the illusion of
1654             # monotonic bug fixing.
1655             my $faking_it;
1656             if (!-f 'hints/darwin.sh') {
1657                 checkout_file('hints/darwin.sh', 'f556e5b971932902');
1658                 ++$faking_it;
1659             }
1660
1661             edit_file('hints/darwin.sh', sub {
1662                       my $code = shift;
1663                       # Part of commit 8f4f83badb7d1ba9, which mostly undoes
1664                       # commit 0511a818910f476c.
1665                       $code =~ s/^cppflags='-traditional-cpp';$/cppflags="\${cppflags} -no-cpp-precomp"/m;
1666                       # commit 14c11978e9b52e08/803bb6cc74d36a3f
1667                       # Without this, code in libperl.bundle links against op.o
1668                       # in preference to opmini.o on the linker command line,
1669                       # and hence miniperl tries to use File::Glob instead of
1670                       # csh
1671                       $code =~ s/^(lddlflags=)/ldflags="\${ldflags} -flat_namespace"\n$1/m;
1672                       # f556e5b971932902 also patches Makefile.SH with some
1673                       # special case code to deal with useshrplib for darwin.
1674                       # Given that post 5.8.0 the darwin hints default was
1675                       # changed to false, and it would be very complex to splice
1676                       # in that code in various versions of Makefile.SH back
1677                       # to 5.002, lets just turn it off.
1678                       $code =~ s/^useshrplib='true'/useshrplib='false'/m
1679                           if $faking_it;
1680
1681                       # Part of commit d235852b65d51c44
1682                       # Don't do this on a case sensitive HFS+ partition, as it
1683                       # breaks the build for 5.003 and earlier.
1684                       if ($case_insensitive
1685                           && $code !~ /^firstmakefile=GNUmakefile/) {
1686                           $code .= "\nfirstmakefile=GNUmakefile;\n";
1687                       }
1688
1689                       return $code;
1690                   });
1691         }
1692     } elsif ($^O eq 'netbsd') {
1693         if ($major < 6) {
1694             # These are part of commit 099685bc64c7dbce
1695             edit_file('hints/netbsd.sh', sub {
1696                           my $code = shift;
1697                           my $fixed = <<'EOC';
1698 case "$osvers" in
1699 0.9|0.8*)
1700         usedl="$undef"
1701         ;;
1702 *)
1703         if [ -f /usr/libexec/ld.elf_so ]; then
1704                 d_dlopen=$define
1705                 d_dlerror=$define
1706                 ccdlflags="-Wl,-E -Wl,-R${PREFIX}/lib $ccdlflags"
1707                 cccdlflags="-DPIC -fPIC $cccdlflags"
1708                 lddlflags="--whole-archive -shared $lddlflags"
1709         elif [ "`uname -m`" = "pmax" ]; then
1710 # NetBSD 1.3 and 1.3.1 on pmax shipped an 'old' ld.so, which will not work.
1711                 d_dlopen=$undef
1712         elif [ -f /usr/libexec/ld.so ]; then
1713                 d_dlopen=$define
1714                 d_dlerror=$define
1715                 ccdlflags="-Wl,-R${PREFIX}/lib $ccdlflags"
1716 # we use -fPIC here because -fpic is *NOT* enough for some of the
1717 # extensions like Tk on some netbsd platforms (the sparc is one)
1718                 cccdlflags="-DPIC -fPIC $cccdlflags"
1719                 lddlflags="-Bforcearchive -Bshareable $lddlflags"
1720         else
1721                 d_dlopen=$undef
1722         fi
1723         ;;
1724 esac
1725 EOC
1726                           $code =~ s/^case "\$osvers" in\n0\.9\|0\.8.*?^esac\n/$fixed/ms;
1727                           return $code;
1728                       });
1729         }
1730     } elsif ($^O eq 'openbsd') {
1731         if ($major < 8) {
1732             checkout_file('hints/openbsd.sh', '43051805d53a3e4c')
1733                 unless -f 'hints/openbsd.sh';
1734             my $which = extract_from_file('hints/openbsd.sh',
1735                                           qr/# from (2\.8|3\.1) onwards/,
1736                                           '');
1737             if ($which eq '') {
1738                 my $was = extract_from_file('hints/openbsd.sh',
1739                                             qr/(lddlflags="(?:-Bforcearchive )?-Bshareable)/);
1740                 # This is commit 154d43cbcf57271c and parts of 5c75dbfa77b0949c
1741                 # and 29b5585702e5e025
1742                 apply_patch(sprintf <<'EOPATCH', $was);
1743 diff --git a/hints/openbsd.sh b/hints/openbsd.sh
1744 index a7d8bf2..5b79709 100644
1745 --- a/hints/openbsd.sh
1746 +++ b/hints/openbsd.sh
1747 @@ -37,7 +37,25 @@ OpenBSD.alpha|OpenBSD.mips|OpenBSD.powerpc|OpenBSD.vax)
1748         # we use -fPIC here because -fpic is *NOT* enough for some of the
1749         # extensions like Tk on some OpenBSD platforms (ie: sparc)
1750         cccdlflags="-DPIC -fPIC $cccdlflags"
1751 -       %s $lddlflags"
1752 +       case "$osvers" in
1753 +       [01].*|2.[0-7]|2.[0-7].*)
1754 +               lddlflags="-Bshareable $lddlflags"
1755 +               ;;
1756 +       2.[8-9]|3.0)
1757 +               ld=${cc:-cc}
1758 +               lddlflags="-shared -fPIC $lddlflags"
1759 +               ;;
1760 +       *) # from 3.1 onwards
1761 +               ld=${cc:-cc}
1762 +               lddlflags="-shared -fPIC $lddlflags"
1763 +               libswanted=`echo $libswanted | sed 's/ dl / /'`
1764 +               ;;
1765 +       esac
1766 +
1767 +       # We need to force ld to export symbols on ELF platforms.
1768 +       # Without this, dlopen() is crippled.
1769 +       ELF=`${cc:-cc} -dM -E - </dev/null | grep __ELF__`
1770 +       test -n "$ELF" && ldflags="-Wl,-E $ldflags"
1771         ;;
1772  esac
1773  
1774 EOPATCH
1775             } elsif ($which eq '2.8') {
1776                 # This is parts of 5c75dbfa77b0949c and 29b5585702e5e025, and
1777                 # possibly eb9cd59d45ad2908
1778                 my $was = extract_from_file('hints/openbsd.sh',
1779                                             qr/lddlflags="(-shared(?: -fPIC)?) \$lddlflags"/);
1780
1781                 apply_patch(sprintf <<'EOPATCH', $was);
1782 --- a/hints/openbsd.sh  2011-10-21 17:25:20.000000000 +0200
1783 +++ b/hints/openbsd.sh  2011-10-21 16:58:43.000000000 +0200
1784 @@ -44,11 +44,21 @@
1785         [01].*|2.[0-7]|2.[0-7].*)
1786                 lddlflags="-Bshareable $lddlflags"
1787                 ;;
1788 -       *) # from 2.8 onwards
1789 +       2.[8-9]|3.0)
1790                 ld=${cc:-cc}
1791 -               lddlflags="%s $lddlflags"
1792 +               lddlflags="-shared -fPIC $lddlflags"
1793 +               ;;
1794 +       *) # from 3.1 onwards
1795 +               ld=${cc:-cc}
1796 +               lddlflags="-shared -fPIC $lddlflags"
1797 +               libswanted=`echo $libswanted | sed 's/ dl / /'`
1798                 ;;
1799         esac
1800 +
1801 +       # We need to force ld to export symbols on ELF platforms.
1802 +       # Without this, dlopen() is crippled.
1803 +       ELF=`${cc:-cc} -dM -E - </dev/null | grep __ELF__`
1804 +       test -n "$ELF" && ldflags="-Wl,-E $ldflags"
1805         ;;
1806  esac
1807  
1808 EOPATCH
1809             } elsif ($which eq '3.1'
1810                      && !extract_from_file('hints/openbsd.sh',
1811                                            qr/We need to force ld to export symbols on ELF platforms/)) {
1812                 # This is part of 29b5585702e5e025
1813                 apply_patch(<<'EOPATCH');
1814 diff --git a/hints/openbsd.sh b/hints/openbsd.sh
1815 index c6b6bc9..4839d04 100644
1816 --- a/hints/openbsd.sh
1817 +++ b/hints/openbsd.sh
1818 @@ -54,6 +54,11 @@ alpha-2.[0-8]|mips-*|vax-*|powerpc-2.[0-7]|m88k-*)
1819                 libswanted=`echo $libswanted | sed 's/ dl / /'`
1820                 ;;
1821         esac
1822 +
1823 +       # We need to force ld to export symbols on ELF platforms.
1824 +       # Without this, dlopen() is crippled.
1825 +       ELF=`${cc:-cc} -dM -E - </dev/null | grep __ELF__`
1826 +       test -n "$ELF" && ldflags="-Wl,-E $ldflags"
1827         ;;
1828  esac
1829  
1830 EOPATCH
1831             }
1832         }
1833     } elsif ($^O eq 'linux') {
1834         if ($major < 1) {
1835             # sparc linux seems to need the -Dbool=char -DHAS_BOOL part of
1836             # perl5.000 patch.0n: [address Configure and build issues]
1837             edit_file('hints/linux.sh', sub {
1838                           my $code = shift;
1839                           $code =~ s!-I/usr/include/bsd!-Dbool=char -DHAS_BOOL!g;
1840                           return $code;
1841                       });
1842         }
1843
1844         if ($major <= 9) {
1845             if (`uname -sm` =~ qr/^Linux sparc/) {
1846                 if (extract_from_file('hints/linux.sh', qr/sparc-linux/)) {
1847                     # Be sure to use -fPIC not -fpic on Linux/SPARC
1848                     apply_commit('f6527d0ef0c13ad4');
1849                 } elsif(!extract_from_file('hints/linux.sh',
1850                                            qr/^sparc-linux\)$/)) {
1851                     my $fh = open_or_die('hints/linux.sh', '>>');
1852                     print $fh <<'EOT' or die $!;
1853
1854 case "`uname -m`" in
1855 sparc*)
1856         case "$cccdlflags" in
1857         *-fpic*) cccdlflags="`echo $cccdlflags|sed 's/-fpic/-fPIC/'`" ;;
1858         *)       cccdlflags="$cccdlflags -fPIC" ;;
1859         esac
1860         ;;
1861 esac
1862 EOT
1863                     close_or_die($fh);
1864                 }
1865             }
1866         }
1867     }
1868 }
1869
1870 sub patch_SH {
1871     # Cwd.xs added in commit 0d2079faa739aaa9. Cwd.pm moved to ext/ 8 years
1872     # later in commit 403f501d5b37ebf0
1873     if ($major > 0 && <*/Cwd/Cwd.xs>) {
1874         if ($major < 10
1875             && !extract_from_file('Makefile.SH', qr/^extra_dep=''$/)) {
1876             # The Makefile.PL for Unicode::Normalize needs
1877             # lib/unicore/CombiningClass.pl. Even without a parallel build, we
1878             # need a dependency to ensure that it builds. This is a variant of
1879             # commit 9f3ef600c170f61e. Putting this for earlier versions gives
1880             # us a spot on which to hang the edits below
1881             apply_patch(<<'EOPATCH');
1882 diff --git a/Makefile.SH b/Makefile.SH
1883 index f61d0db..6097954 100644
1884 --- a/Makefile.SH
1885 +++ b/Makefile.SH
1886 @@ -155,10 +155,20 @@ esac
1887  
1888  : Prepare dependency lists for Makefile.
1889  dynamic_list=' '
1890 +extra_dep=''
1891  for f in $dynamic_ext; do
1892      : the dependency named here will never exist
1893        base=`echo "$f" | sed 's/.*\///'`
1894 -    dynamic_list="$dynamic_list lib/auto/$f/$base.$dlext"
1895 +    this_target="lib/auto/$f/$base.$dlext"
1896 +    dynamic_list="$dynamic_list $this_target"
1897 +
1898 +    : Parallel makes reveal that we have some interdependencies
1899 +    case $f in
1900 +       Math/BigInt/FastCalc) extra_dep="$extra_dep
1901 +$this_target: lib/auto/List/Util/Util.$dlext" ;;
1902 +       Unicode/Normalize) extra_dep="$extra_dep
1903 +$this_target: lib/unicore/CombiningClass.pl" ;;
1904 +    esac
1905  done
1906  
1907  static_list=' '
1908 @@ -987,2 +997,9 @@ n_dummy $(nonxs_ext):       miniperl$(EXE_EXT) preplibrary $(DYNALOADER) FORCE
1909         @$(LDLIBPTH) sh ext/util/make_ext nonxs $@ MAKE=$(MAKE) LIBPERL_A=$(LIBPERL)
1910 +!NO!SUBS!
1911 +
1912 +$spitshell >>Makefile <<EOF
1913 +$extra_dep
1914 +EOF
1915 +
1916 +$spitshell >>Makefile <<'!NO!SUBS!'
1917  
1918 EOPATCH
1919         }
1920
1921         if ($major == 11) {
1922             if (extract_from_file('patchlevel.h',
1923                                   qr/^#include "unpushed\.h"/)) {
1924                 # I had thought it easier to detect when building one of the 52
1925                 # commits with the original method of incorporating the git
1926                 # revision and drop parallel make flags. Commits shown by
1927                 # git log 46807d8e809cc127^..dcff826f70bf3f64^ ^d4fb0a1f15d1a1c4
1928                 # However, it's not actually possible to make miniperl for that
1929                 # configuration as-is, because the file .patchnum is only made
1930                 # as a side effect of target 'all'
1931                 # I also don't think that it's "safe" to simply run
1932                 # make_patchnum.sh before the build. We need the proper
1933                 # dependency rules in the Makefile to *stop* it being run again
1934                 # at the wrong time.
1935                 # This range is important because contains the commit that
1936                 # merges Schwern's y2038 work.
1937                 apply_patch(<<'EOPATCH');
1938 diff --git a/Makefile.SH b/Makefile.SH
1939 index 9ad8b6f..106e721 100644
1940 --- a/Makefile.SH
1941 +++ b/Makefile.SH
1942 @@ -540,9 +544,14 @@ sperl.i: perl.c $(h)
1943  
1944  .PHONY: all translators utilities make_patchnum
1945  
1946 -make_patchnum:
1947 +make_patchnum: lib/Config_git.pl
1948 +
1949 +lib/Config_git.pl: make_patchnum.sh
1950         sh $(shellflags) make_patchnum.sh
1951  
1952 +# .patchnum, unpushed.h and lib/Config_git.pl are built by make_patchnum.sh
1953 +unpushed.h .patchnum: lib/Config_git.pl
1954 +
1955  # make sure that we recompile perl.c if .patchnum changes
1956  perl$(OBJ_EXT): .patchnum unpushed.h
1957  
1958 EOPATCH
1959             } elsif (-f '.gitignore'
1960                      && extract_from_file('.gitignore', qr/^\.patchnum$/)) {
1961                 # 8565263ab8a47cda to 46807d8e809cc127^ inclusive.
1962                 edit_file('Makefile.SH', sub {
1963                               my $code = shift;
1964                               $code =~ s/^make_patchnum:\n/make_patchnum: .patchnum
1965
1966 .sha1: .patchnum
1967
1968 .patchnum: make_patchnum.sh
1969 /m;
1970                               return $code;
1971                           });
1972             } elsif (-f 'lib/.gitignore'
1973                      && extract_from_file('lib/.gitignore',
1974                                           qr!^/Config_git.pl!)
1975                      && !extract_from_file('Makefile.SH',
1976                                         qr/^uudmap\.h.*:bitcount.h$/)) {
1977                 # Between commits and dcff826f70bf3f64 and 0f13ebd5d71f8177^
1978                 edit_file('Makefile.SH', sub {
1979                               my $code = shift;
1980                               # Bug introduced by 344af494c35a9f0f
1981                               # fixed in 0f13ebd5d71f8177
1982                               $code =~ s{^(pod/perlapi\.pod) (pod/perlintern\.pod): }
1983                                         {$1: $2\n\n$2: }m;
1984                               # Bug introduced by efa50c51e3301a2c
1985                               # fixed in 0f13ebd5d71f8177
1986                               $code =~ s{^(uudmap\.h) (bitcount\.h): }
1987                                         {$1: $2\n\n$2: }m;
1988
1989                               # The rats nest of getting git_version.h correct
1990
1991                               if ($code =~ s{git_version\.h: stock_git_version\.h
1992 \tcp stock_git_version\.h git_version\.h}
1993                                             {}m) {
1994                                   # before 486cd780047ff224
1995
1996                                   # We probably can't build between
1997                                   # 953f6acfa20ec275^ and 8565263ab8a47cda
1998                                   # inclusive, but all commits in that range
1999                                   # relate to getting make_patchnum.sh working,
2000                                   # so it is extremely unlikely to be an
2001                                   # interesting bisect target. They will skip.
2002
2003                                   # No, don't spawn a submake if
2004                                   # make_patchnum.sh or make_patchnum.pl fails
2005                                   $code =~ s{\|\| \$\(MAKE\) miniperl.*}
2006                                             {}m;
2007                                   $code =~ s{^\t(sh.*make_patchnum\.sh.*)}
2008                                             {\t-$1}m;
2009
2010                                   # Use an external perl to run make_patchnum.pl
2011                                   # because miniperl still depends on
2012                                   # git_version.h
2013                                   $code =~ s{^\t.*make_patchnum\.pl}
2014                                             {\t-$^X make_patchnum.pl}m;
2015
2016
2017                                   # "Truth in advertising" - running
2018                                   # make_patchnum generates 2 files.
2019                                   $code =~ s{^make_patchnum:.*}{
2020 make_patchnum: lib/Config_git.pl
2021
2022 git_version.h: lib/Config_git.pl
2023
2024 perlmini\$(OBJ_EXT): git_version.h
2025
2026 lib/Config_git.pl:}m;
2027                               }
2028                               # Right, now we've corrected Makefile.SH to
2029                               # correctly describe how lib/Config_git.pl and
2030                               # git_version.h are made, we need to fix the rest
2031
2032                               # This emulates commit 2b63e250843b907e
2033                               # This might duplicate the rule stating that
2034                               # git_version.h depends on lib/Config_git.pl
2035                               # This is harmless.
2036                               $code =~ s{^(?:lib/Config_git\.pl )?git_version\.h: (.* make_patchnum\.pl.*)}
2037                                         {git_version.h: lib/Config_git.pl
2038
2039 lib/Config_git.pl: $1}m;
2040
2041                               # This emulates commits 0f13ebd5d71f8177 and
2042                               # and a04d4598adc57886. It ensures that
2043                               # lib/Config_git.pl is built before configpm,
2044                               # and that configpm is run exactly once.
2045                               $code =~ s{^(\$\(.*?\) )?(\$\(CONFIGPOD\))(: .*? configpm Porting/Glossary)( lib/Config_git\.pl)?}{
2046                                   # If present, other files depend on $(CONFIGPOD)
2047                                   ($1 ? "$1: $2\n\n" : '')
2048                                       # Then the rule we found
2049                                       . $2 . $3
2050                                           # Add dependency if not there
2051                                           . ($4 ? $4 : ' lib/Config_git.pl')
2052                               }me;
2053
2054                               return $code;
2055                           });
2056             }
2057         }
2058
2059         if ($major < 14) {
2060             # Commits dc0655f797469c47 and d11a62fe01f2ecb2
2061             edit_file('Makefile.SH', sub {
2062                           my $code = shift;
2063                           foreach my $ext (qw(Encode SDBM_File)) {
2064                               next if $code =~ /\b$ext\) extra_dep=/s;
2065                               $code =~ s!(\) extra_dep="\$extra_dep
2066 \$this_target: .*?" ;;)
2067 (    esac
2068 )!$1
2069         $ext) extra_dep="\$extra_dep
2070 \$this_target: lib/auto/Cwd/Cwd.\$dlext" ;;
2071 $2!;
2072                           }
2073                           return $code;
2074                       });
2075         }
2076     }
2077
2078     if ($major == 7) {
2079         # Remove commits 9fec149bb652b6e9 and 5bab1179608f81d8, which add/amend
2080         # rules to automatically run regen scripts that rebuild C headers. These
2081         # cause problems because a git checkout doesn't preserve relative file
2082         # modification times, hence the regen scripts may fire. This will
2083         # obscure whether the repository had the correct generated headers
2084         # checked in.
2085         # Also, the dependency rules for running the scripts were not correct,
2086         # which could cause spurious re-builds on re-running make, and can cause
2087         # complete build failures for a parallel make.
2088         if (extract_from_file('Makefile.SH',
2089                               qr/Writing it this way gives make a big hint to always run opcode\.pl before/)) {
2090             apply_commit('70c6e6715e8fec53');
2091         } elsif (extract_from_file('Makefile.SH',
2092                                    qr/^opcode\.h opnames\.h pp_proto\.h pp\.sym: opcode\.pl$/)) {
2093             revert_commit('9fec149bb652b6e9');
2094         }
2095     }
2096
2097     if ($^O eq 'aix' && $major >= 11 && $major <= 15
2098         && extract_from_file('makedef.pl', qr/^use Config/)) {
2099         edit_file('Makefile.SH', sub {
2100                       # The AIX part of commit e6807d8ab22b761c
2101                       # It's safe to substitute lib/Config.pm for config.sh
2102                       # as lib/Config.pm depends on config.sh
2103                       # If the tree is post e6807d8ab22b761c, the substitution
2104                       # won't match, which is harmless.
2105                       my $code = shift;
2106                       $code =~ s{^(perl\.exp:.* )config\.sh(\b.*)}
2107                                 {$1 . '$(CONFIGPM)' . $2}me;
2108                       return $code;
2109                   });
2110     }
2111
2112     # There was a bug in makedepend.SH which was fixed in version 96a8704c.
2113     # Symptom was './makedepend: 1: Syntax error: Unterminated quoted string'
2114     # Remove this if you're actually bisecting a problem related to
2115     # makedepend.SH
2116     # If you do this, you may need to add in code to correct the output of older
2117     # makedepends, which don't correctly filter newer gcc output such as
2118     # <built-in>
2119     checkout_file('makedepend.SH');
2120
2121     if ($major < 4 && -f 'config.sh'
2122         && !extract_from_file('config.sh', qr/^trnl=/)) {
2123         # This seems to be necessary to avoid makedepend becoming confused,
2124         # and hanging on stdin. Seems that the code after
2125         # make shlist || ...here... is never run.
2126         edit_file('makedepend.SH', sub {
2127                       my $code = shift;
2128                       $code =~ s/^trnl='\$trnl'$/trnl='\\n'/m;
2129                       return $code;
2130                   });
2131     }
2132 }
2133
2134 sub patch_C {
2135     # This is ordered by $major, as it's likely that different platforms may
2136     # well want to share code.
2137
2138     if ($major == 2 && extract_from_file('perl.c', qr/^\tfclose\(e_fp\);$/)) {
2139         # need to patch perl.c to avoid calling fclose() twice on e_fp when
2140         # using -e
2141         # This diff is part of commit ab821d7fdc14a438. The second close was
2142         # introduced with perl-5.002, commit a5f75d667838e8e7
2143         # Might want a6c477ed8d4864e6 too, for the corresponding change to
2144         # pp_ctl.c (likely without this, eval will have "fun")
2145         apply_patch(<<'EOPATCH');
2146 diff --git a/perl.c b/perl.c
2147 index 03c4d48..3c814a2 100644
2148 --- a/perl.c
2149 +++ b/perl.c
2150 @@ -252,6 +252,7 @@ setuid perl scripts securely.\n");
2151  #ifndef VMS  /* VMS doesn't have environ array */
2152      origenviron = environ;
2153  #endif
2154 +    e_tmpname = Nullch;
2155  
2156      if (do_undump) {
2157  
2158 @@ -405,6 +406,7 @@ setuid perl scripts securely.\n");
2159      if (e_fp) {
2160         if (Fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
2161             croak("Can't write to temp file for -e: %s", Strerror(errno));
2162 +       e_fp = Nullfp;
2163         argc++,argv--;
2164         scriptname = e_tmpname;
2165      }
2166 @@ -470,10 +472,10 @@ setuid perl scripts securely.\n");
2167      curcop->cop_line = 0;
2168      curstash = defstash;
2169      preprocess = FALSE;
2170 -    if (e_fp) {
2171 -       fclose(e_fp);
2172 -       e_fp = Nullfp;
2173 +    if (e_tmpname) {
2174         (void)UNLINK(e_tmpname);
2175 +       Safefree(e_tmpname);
2176 +       e_tmpname = Nullch;
2177      }
2178  
2179      /* now that script is parsed, we can modify record separator */
2180 @@ -1369,7 +1371,7 @@ SV *sv;
2181         scriptname = xfound;
2182      }
2183  
2184 -    origfilename = savepv(e_fp ? "-e" : scriptname);
2185 +    origfilename = savepv(e_tmpname ? "-e" : scriptname);
2186      curcop->cop_filegv = gv_fetchfile(origfilename);
2187      if (strEQ(origfilename,"-"))
2188         scriptname = "";
2189
2190 EOPATCH
2191     }
2192
2193     if ($major < 3 && $^O eq 'openbsd'
2194         && !extract_from_file('pp_sys.c', qr/BSD_GETPGRP/)) {
2195         # Part of commit c3293030fd1b7489
2196         apply_patch(<<'EOPATCH');
2197 diff --git a/pp_sys.c b/pp_sys.c
2198 index 4608a2a..f0c9d1d 100644
2199 --- a/pp_sys.c
2200 +++ b/pp_sys.c
2201 @@ -2903,8 +2903,8 @@ PP(pp_getpgrp)
2202         pid = 0;
2203      else
2204         pid = SvIVx(POPs);
2205 -#ifdef USE_BSDPGRP
2206 -    value = (I32)getpgrp(pid);
2207 +#ifdef BSD_GETPGRP
2208 +    value = (I32)BSD_GETPGRP(pid);
2209  #else
2210      if (pid != 0)
2211         DIE("POSIX getpgrp can't take an argument");
2212 @@ -2933,8 +2933,8 @@ PP(pp_setpgrp)
2213      }
2214  
2215      TAINT_PROPER("setpgrp");
2216 -#ifdef USE_BSDPGRP
2217 -    SETi( setpgrp(pid, pgrp) >= 0 );
2218 +#ifdef BSD_SETPGRP
2219 +    SETi( BSD_SETPGRP(pid, pgrp) >= 0 );
2220  #else
2221      if ((pgrp != 0) || (pid != 0)) {
2222         DIE("POSIX setpgrp can't take an argument");
2223 EOPATCH
2224     }
2225
2226     if ($major < 4 && $^O eq 'openbsd') {
2227         my $bad;
2228         # Need changes from commit a6e633defa583ad5.
2229         # Commits c07a80fdfe3926b5 and f82b3d4130164d5f changed the same part
2230         # of perl.h
2231
2232         if (extract_from_file('perl.h',
2233                               qr/^#ifdef HAS_GETPGRP2$/)) {
2234             $bad = <<'EOBAD';
2235 ***************
2236 *** 57,71 ****
2237   #define TAINT_PROPER(s)       if (tainting) taint_proper(no_security, s)
2238   #define TAINT_ENV()   if (tainting) taint_env()
2239   
2240 ! #ifdef HAS_GETPGRP2
2241 ! #   ifndef HAS_GETPGRP
2242 ! #     define HAS_GETPGRP
2243 ! #   endif
2244 ! #endif
2245
2246 ! #ifdef HAS_SETPGRP2
2247 ! #   ifndef HAS_SETPGRP
2248 ! #     define HAS_SETPGRP
2249 ! #   endif
2250   #endif
2251   
2252 EOBAD
2253         } elsif (extract_from_file('perl.h',
2254                                    qr/Gack, you have one but not both of getpgrp2/)) {
2255             $bad = <<'EOBAD';
2256 ***************
2257 *** 56,76 ****
2258   #define TAINT_PROPER(s)       if (tainting) taint_proper(no_security, s)
2259   #define TAINT_ENV()   if (tainting) taint_env()
2260   
2261 ! #if defined(HAS_GETPGRP2) && defined(HAS_SETPGRP2)
2262 ! #   define getpgrp getpgrp2
2263 ! #   define setpgrp setpgrp2
2264 ! #   ifndef HAS_GETPGRP
2265 ! #     define HAS_GETPGRP
2266 ! #   endif
2267 ! #   ifndef HAS_SETPGRP
2268 ! #     define HAS_SETPGRP
2269 ! #   endif
2270 ! #   ifndef USE_BSDPGRP
2271 ! #     define USE_BSDPGRP
2272 ! #   endif
2273 ! #else
2274 ! #   if defined(HAS_GETPGRP2) || defined(HAS_SETPGRP2)
2275 !       #include "Gack, you have one but not both of getpgrp2() and setpgrp2()."
2276 ! #   endif
2277   #endif
2278   
2279 EOBAD
2280         } elsif (extract_from_file('perl.h',
2281                                    qr/^#ifdef USE_BSDPGRP$/)) {
2282             $bad = <<'EOBAD'
2283 ***************
2284 *** 91,116 ****
2285   #define TAINT_PROPER(s)       if (tainting) taint_proper(no_security, s)
2286   #define TAINT_ENV()   if (tainting) taint_env()
2287   
2288 ! #ifdef USE_BSDPGRP
2289 ! #   ifdef HAS_GETPGRP
2290 ! #       define BSD_GETPGRP(pid) getpgrp((pid))
2291 ! #   endif
2292 ! #   ifdef HAS_SETPGRP
2293 ! #       define BSD_SETPGRP(pid, pgrp) setpgrp((pid), (pgrp))
2294 ! #   endif
2295 ! #else
2296 ! #   ifdef HAS_GETPGRP2
2297 ! #       define BSD_GETPGRP(pid) getpgrp2((pid))
2298 ! #       ifndef HAS_GETPGRP
2299 ! #         define HAS_GETPGRP
2300 ! #     endif
2301 ! #   endif
2302 ! #   ifdef HAS_SETPGRP2
2303 ! #       define BSD_SETPGRP(pid, pgrp) setpgrp2((pid), (pgrp))
2304 ! #       ifndef HAS_SETPGRP
2305 ! #         define HAS_SETPGRP
2306 ! #     endif
2307 ! #   endif
2308   #endif
2309   
2310   #ifndef _TYPES_               /* If types.h defines this it's easy. */
2311 EOBAD
2312         }
2313         if ($bad) {
2314             apply_patch(<<"EOPATCH");
2315 *** a/perl.h    2011-10-21 09:46:12.000000000 +0200
2316 --- b/perl.h    2011-10-21 09:46:12.000000000 +0200
2317 $bad--- 91,144 ----
2318   #define TAINT_PROPER(s)       if (tainting) taint_proper(no_security, s)
2319   #define TAINT_ENV()   if (tainting) taint_env()
2320   
2321 ! /* XXX All process group stuff is handled in pp_sys.c.  Should these 
2322 !    defines move there?  If so, I could simplify this a lot. --AD  9/96.
2323 ! */
2324 ! /* Process group stuff changed from traditional BSD to POSIX.
2325 !    perlfunc.pod documents the traditional BSD-style syntax, so we'll
2326 !    try to preserve that, if possible.
2327 ! */
2328 ! #ifdef HAS_SETPGID
2329 ! #  define BSD_SETPGRP(pid, pgrp)      setpgid((pid), (pgrp))
2330 ! #else
2331 ! #  if defined(HAS_SETPGRP) && defined(USE_BSD_SETPGRP)
2332 ! #    define BSD_SETPGRP(pid, pgrp)    setpgrp((pid), (pgrp))
2333 ! #  else
2334 ! #    ifdef HAS_SETPGRP2  /* DG/UX */
2335 ! #      define BSD_SETPGRP(pid, pgrp)  setpgrp2((pid), (pgrp))
2336 ! #    endif
2337 ! #  endif
2338 ! #endif
2339 ! #if defined(BSD_SETPGRP) && !defined(HAS_SETPGRP)
2340 ! #  define HAS_SETPGRP  /* Well, effectively it does . . . */
2341 ! #endif
2342
2343 ! /* getpgid isn't POSIX, but at least Solaris and Linux have it, and it makes
2344 !     our life easier :-) so we'll try it.
2345 ! */
2346 ! #ifdef HAS_GETPGID
2347 ! #  define BSD_GETPGRP(pid)            getpgid((pid))
2348 ! #else
2349 ! #  if defined(HAS_GETPGRP) && defined(USE_BSD_GETPGRP)
2350 ! #    define BSD_GETPGRP(pid)          getpgrp((pid))
2351 ! #  else
2352 ! #    ifdef HAS_GETPGRP2  /* DG/UX */
2353 ! #      define BSD_GETPGRP(pid)                getpgrp2((pid))
2354 ! #    endif
2355 ! #  endif
2356 ! #endif
2357 ! #if defined(BSD_GETPGRP) && !defined(HAS_GETPGRP)
2358 ! #  define HAS_GETPGRP  /* Well, effectively it does . . . */
2359 ! #endif
2360
2361 ! /* These are not exact synonyms, since setpgrp() and getpgrp() may 
2362 !    have different behaviors, but perl.h used to define USE_BSDPGRP
2363 !    (prior to 5.003_05) so some extension might depend on it.
2364 ! */
2365 ! #if defined(USE_BSD_SETPGRP) || defined(USE_BSD_GETPGRP)
2366 ! #  ifndef USE_BSDPGRP
2367 ! #    define USE_BSDPGRP
2368 ! #  endif
2369   #endif
2370   
2371   #ifndef _TYPES_               /* If types.h defines this it's easy. */
2372 EOPATCH
2373         }
2374     }
2375
2376     if ($major == 4 && extract_from_file('scope.c', qr/\(SV\*\)SSPOPINT/)) {
2377         # [PATCH] 5.004_04 +MAINT_TRIAL_1 broken when sizeof(int) != sizeof(void)
2378         # Fixes a bug introduced in 161b7d1635bc830b
2379         apply_commit('9002cb76ec83ef7f');
2380     }
2381
2382     if ($major == 4 && extract_from_file('av.c', qr/AvARRAY\(av\) = 0;/)) {
2383         # Fixes a bug introduced in 1393e20655efb4bc
2384         apply_commit('e1c148c28bf3335b', 'av.c');
2385     }
2386
2387     if ($major == 4) {
2388         my $rest = extract_from_file('perl.c', qr/delimcpy(.*)/);
2389         if (defined $rest and $rest !~ /,$/) {
2390             # delimcpy added in fc36a67e8855d031, perl.c refactored to use it.
2391             # bug introduced in 2a92aaa05aa1acbf, fixed in 8490252049bf42d3
2392             # code then moved to util.c in commit 491527d0220de34e
2393             apply_patch(<<'EOPATCH');
2394 diff --git a/perl.c b/perl.c
2395 index 4eb69e3..54bbb00 100644
2396 --- a/perl.c
2397 +++ b/perl.c
2398 @@ -1735,7 +1735,7 @@ SV *sv;
2399             if (len < sizeof tokenbuf)
2400                 tokenbuf[len] = '\0';
2401  #else  /* ! (atarist || DOSISH) */
2402 -           s = delimcpy(tokenbuf, tokenbuf + sizeof tokenbuf, s, bufend
2403 +           s = delimcpy(tokenbuf, tokenbuf + sizeof tokenbuf, s, bufend,
2404                          ':',
2405                          &len);
2406  #endif /* ! (atarist || DOSISH) */
2407 EOPATCH
2408         }
2409     }
2410
2411     if ($major == 4 && $^O eq 'linux') {
2412         # Whilst this is fixed properly in f0784f6a4c3e45e1 which provides the
2413         # Configure probe, it's easier to back out the problematic changes made
2414         # in these previous commits:
2415         if (extract_from_file('doio.c',
2416                               qr!^/\* XXX REALLY need metaconfig test \*/$!)) {
2417             revert_commit('4682965a1447ea44', 'doio.c');
2418         }
2419         if (my $token = extract_from_file('doio.c',
2420                                           qr!^#if (defined\(__sun(?:__)?\)) && defined\(__svr4__\) /\* XXX Need metaconfig test \*/$!)) {
2421             my $patch = `git show -R 9b599b2a63d2324d doio.c`;
2422             $patch =~ s/defined\(__sun__\)/$token/g;
2423             apply_patch($patch);
2424         }
2425         if (extract_from_file('doio.c',
2426                               qr!^/\* linux \(and Solaris2\?\) uses :$!)) {
2427             revert_commit('8490252049bf42d3', 'doio.c');
2428         }
2429         if (extract_from_file('doio.c',
2430                               qr/^          unsemds.buf = &semds;$/)) {
2431             revert_commit('8e591e46b4c6543e');
2432         }
2433         if (extract_from_file('doio.c',
2434                               qr!^#ifdef __linux__      /\* XXX Need metaconfig test \*/$!)) {
2435             # Reverts part of commit 3e3baf6d63945cb6
2436             apply_patch(<<'EOPATCH');
2437 diff --git b/doio.c a/doio.c
2438 index 62b7de9..0d57425 100644
2439 --- b/doio.c
2440 +++ a/doio.c
2441 @@ -1333,9 +1331,6 @@ SV **sp;
2442      char *a;
2443      I32 id, n, cmd, infosize, getinfo;
2444      I32 ret = -1;
2445 -#ifdef __linux__       /* XXX Need metaconfig test */
2446 -    union semun unsemds;
2447 -#endif
2448  
2449      id = SvIVx(*++mark);
2450      n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2451 @@ -1364,29 +1359,11 @@ SV **sp;
2452             infosize = sizeof(struct semid_ds);
2453         else if (cmd == GETALL || cmd == SETALL)
2454         {
2455 -#ifdef __linux__       /* XXX Need metaconfig test */
2456 -/* linux uses :
2457 -   int semctl (int semid, int semnun, int cmd, union semun arg)
2458 -
2459 -       union semun {
2460 -            int val;
2461 -            struct semid_ds *buf;
2462 -            ushort *array;
2463 -       };
2464 -*/
2465 -            union semun semds;
2466 -           if (semctl(id, 0, IPC_STAT, semds) == -1)
2467 -#else
2468             struct semid_ds semds;
2469             if (semctl(id, 0, IPC_STAT, &semds) == -1)
2470 -#endif
2471                 return -1;
2472             getinfo = (cmd == GETALL);
2473 -#ifdef __linux__       /* XXX Need metaconfig test */
2474 -           infosize = semds.buf->sem_nsems * sizeof(short);
2475 -#else
2476             infosize = semds.sem_nsems * sizeof(short);
2477 -#endif
2478                 /* "short" is technically wrong but much more portable
2479                    than guessing about u_?short(_t)? */
2480         }
2481 @@ -1429,12 +1406,7 @@ SV **sp;
2482  #endif
2483  #ifdef HAS_SEM
2484      case OP_SEMCTL:
2485 -#ifdef __linux__       /* XXX Need metaconfig test */
2486 -        unsemds.buf = (struct semid_ds *)a;
2487 -       ret = semctl(id, n, cmd, unsemds);
2488 -#else
2489         ret = semctl(id, n, cmd, (struct semid_ds *)a);
2490 -#endif
2491         break;
2492  #endif
2493  #ifdef HAS_SHM
2494 EOPATCH
2495         }
2496         # Incorrect prototype added as part of 8ac853655d9b7447, fixed as part
2497         # of commit dc45a647708b6c54, with at least one intermediate
2498         # modification. Correct prototype for gethostbyaddr has socklen_t
2499         # second. Linux has uint32_t first for getnetbyaddr.
2500         # Easiest just to remove, instead of attempting more complex patching.
2501         # Something similar may be needed on other platforms.
2502         edit_file('pp_sys.c', sub {
2503                       my $code = shift;
2504                       $code =~ s/^    struct hostent \*(?:PerlSock_)?gethostbyaddr\([^)]+\);$//m;
2505                       $code =~ s/^    struct netent \*getnetbyaddr\([^)]+\);$//m;
2506                       return $code;
2507                   });
2508     }
2509
2510     if ($major < 5 && $^O eq 'aix'
2511         && !extract_from_file('pp_sys.c',
2512                               qr/defined\(HOST_NOT_FOUND\) && !defined\(h_errno\)/)) {
2513         # part of commit dc45a647708b6c54
2514         # Andy Dougherty's configuration patches (Config_63-01 up to 04).
2515         apply_patch(<<'EOPATCH')
2516 diff --git a/pp_sys.c b/pp_sys.c
2517 index c2fcb6f..efa39fb 100644
2518 --- a/pp_sys.c
2519 +++ b/pp_sys.c
2520 @@ -54,7 +54,7 @@ extern "C" int syscall(unsigned long,...);
2521  #endif
2522  #endif
2523  
2524 -#ifdef HOST_NOT_FOUND
2525 +#if defined(HOST_NOT_FOUND) && !defined(h_errno)
2526  extern int h_errno;
2527  #endif
2528  
2529 EOPATCH
2530     }
2531
2532     if ($major == 5
2533         && `git rev-parse HEAD` eq "22c35a8c2392967a5ba6b5370695be464bd7012c\n") {
2534         # Commit 22c35a8c2392967a is significant,
2535         # "phase 1 of somewhat major rearrangement of PERL_OBJECT stuff"
2536         # but doesn't build due to 2 simple errors. blead in this broken state
2537         # was merged to the cfgperl branch, and then these were immediately
2538         # corrected there. cfgperl (with the fixes) was merged back to blead.
2539         # The resultant rather twisty maze of commits looks like this:
2540
2541 =begin comment
2542
2543 * | |   commit 137225782c183172f360c827424b9b9f8adbef0e
2544 |\ \ \  Merge: 22c35a8 2a8ee23
2545 | |/ /  Author: Gurusamy Sarathy <gsar@cpan.org>
2546 | | |   Date:   Fri Oct 30 17:38:36 1998 +0000
2547 | | |
2548 | | |       integrate cfgperl tweaks into mainline
2549 | | |
2550 | | |       p4raw-id: //depot/perl@2144
2551 | | |
2552 | * | commit 2a8ee23279873759693fa83eca279355db2b665c
2553 | | | Author: Jarkko Hietaniemi <jhi@iki.fi>
2554 | | | Date:   Fri Oct 30 13:27:39 1998 +0000
2555 | | |
2556 | | |     There can be multiple yacc/bison errors.
2557 | | |
2558 | | |     p4raw-id: //depot/cfgperl@2143
2559 | | |
2560 | * | commit 93fb2ac393172fc3e2c14edb20b718309198abbc
2561 | | | Author: Jarkko Hietaniemi <jhi@iki.fi>
2562 | | | Date:   Fri Oct 30 13:18:43 1998 +0000
2563 | | |
2564 | | |     README.posix-bc update.
2565 | | |
2566 | | |     p4raw-id: //depot/cfgperl@2142
2567 | | |
2568 | * | commit 4ec43091e8e6657cb260b5e563df30aaa154effe
2569 | | | Author: Jarkko Hietaniemi <jhi@iki.fi>
2570 | | | Date:   Fri Oct 30 09:12:59 1998 +0000
2571 | | |
2572 | | |     #2133 fallout.
2573 | | |
2574 | | |     p4raw-id: //depot/cfgperl@2141
2575 | | |
2576 | * |   commit 134ca994cfefe0f613d43505a885e4fc2100b05c
2577 | |\ \  Merge: 7093112 22c35a8
2578 | |/ /  Author: Jarkko Hietaniemi <jhi@iki.fi>
2579 |/| |   Date:   Fri Oct 30 08:43:18 1998 +0000
2580 | | |
2581 | | |       Integrate from mainperl.
2582 | | |
2583 | | |       p4raw-id: //depot/cfgperl@2140
2584 | | |
2585 * | | commit 22c35a8c2392967a5ba6b5370695be464bd7012c
2586 | | | Author: Gurusamy Sarathy <gsar@cpan.org>
2587 | | | Date:   Fri Oct 30 02:51:39 1998 +0000
2588 | | |
2589 | | |     phase 1 of somewhat major rearrangement of PERL_OBJECT stuff
2590 | | |     (objpp.h is gone, embed.pl now does some of that); objXSUB.h
2591 | | |     should soon be automated also; the global variables that
2592 | | |     escaped the PL_foo conversion are now reined in; renamed
2593 | | |     MAGIC in regcomp.h to REG_MAGIC to avoid collision with the
2594 | | |     type of same name; duplicated lists of pp_things in various
2595 | | |     places is now gone; result has only been tested on win32
2596 | | |
2597 | | |     p4raw-id: //depot/perl@2133
2598
2599 =end comment
2600
2601 =cut
2602
2603         # and completely confuses git bisect (and at least me), causing it to
2604         # the bisect run to confidently return the wrong answer, an unrelated
2605         # commit on the cfgperl branch.
2606
2607         apply_commit('4ec43091e8e6657c');
2608     }
2609
2610     if ($major == 5
2611         && extract_from_file('pp_sys.c', qr/PERL_EFF_ACCESS_R_OK/)
2612         && !extract_from_file('pp_sys.c', qr/XXX Configure test needed for eaccess/)) {
2613         # Between 5ff3f7a4e03a6b10 and c955f1177b2e311d^
2614         # This is the meat of commit c955f1177b2e311d (without the other
2615         # indenting changes that would cause a conflict).
2616         # Without this 538 revisions won't build on (at least) Linux
2617         apply_patch(<<'EOPATCH');
2618 diff --git a/pp_sys.c b/pp_sys.c
2619 index d60c8dc..867dee4 100644
2620 --- a/pp_sys.c
2621 +++ b/pp_sys.c
2622 @@ -198,9 +198,18 @@ static char zero_but_true[ZBTLEN + 1] = "0 but true";
2623  #   if defined(I_SYS_SECURITY)
2624  #       include <sys/security.h>
2625  #   endif
2626 -#   define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
2627 -#   define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
2628 -#   define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
2629 +    /* XXX Configure test needed for eaccess */
2630 +#   ifdef ACC_SELF
2631 +        /* HP SecureWare */
2632 +#       define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK, ACC_SELF))
2633 +#       define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK, ACC_SELF))
2634 +#       define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK, ACC_SELF))
2635 +#   else
2636 +        /* SCO */
2637 +#       define PERL_EFF_ACCESS_R_OK(p) (eaccess((p), R_OK))
2638 +#       define PERL_EFF_ACCESS_W_OK(p) (eaccess((p), W_OK))
2639 +#       define PERL_EFF_ACCESS_X_OK(p) (eaccess((p), X_OK))
2640 +#   endif
2641  #endif
2642  
2643  #if !defined(PERL_EFF_ACCESS_R_OK) && defined(HAS_ACCESSX) && defined(ACC_SELF)
2644 EOPATCH
2645     }
2646
2647     if ($major == 5
2648         && extract_from_file('mg.c', qr/If we're still on top of the stack, pop us off/)
2649         && !extract_from_file('mg.c', qr/PL_savestack_ix -= popval/)) {
2650         # Fix up commit 455ece5e082708b1:
2651         # SSNEW() API for allocating memory on the savestack
2652         # Message-Id: <tqemtae338.fsf@puma.genscan.com>
2653         # Subject: [PATCH 5.005_51] (was: why SAVEDESTRUCTOR()...)
2654         apply_commit('3c8a44569607336e', 'mg.c');
2655     }
2656
2657     if ($major == 5) {
2658         if (extract_from_file('doop.c', qr/croak\(no_modify\);/)
2659             && extract_from_file('doop.c', qr/croak\(PL_no_modify\);/)) {
2660             # Whilst the log suggests that this would only fix 5 commits, in
2661             # practice this area of history is a complete tarpit, and git bisect
2662             # gets very confused by the skips in the middle of the back and
2663             # forth merging between //depot/perl and //depot/cfgperl
2664             apply_commit('6393042b638dafd3');
2665         }
2666
2667         # One error "fixed" with another:
2668         if (extract_from_file('pp_ctl.c',
2669                               qr/\Qstatic void *docatch_body _((void *o));\E/)) {
2670             apply_commit('5b51e982882955fe');
2671         }
2672         # Which is then fixed by this:
2673         if (extract_from_file('pp_ctl.c',
2674                               qr/\Qstatic void *docatch_body _((valist\E/)) {
2675             apply_commit('47aa779ee4c1a50e');
2676         }
2677
2678         if (extract_from_file('thrdvar.h', qr/PERLVARI\(Tprotect/)
2679             && !extract_from_file('embedvar.h', qr/PL_protect/)) {
2680             # Commit 312caa8e97f1c7ee didn't update embedvar.h
2681             apply_commit('e0284a306d2de082', 'embedvar.h');
2682         }
2683     }
2684
2685     if ($major == 5
2686         && extract_from_file('sv.c',
2687                              qr/PerlDir_close\(IoDIRP\((?:\(IO\*\))?sv\)\);/)
2688         && !(extract_from_file('toke.c',
2689                                qr/\QIoDIRP(FILTER_DATA(AvFILLp(PL_rsfp_filters))) = NULL\E/)
2690              || extract_from_file('toke.c',
2691                                   qr/\QIoDIRP(datasv) = (DIR*)NULL;\E/))) {
2692         # Commit 93578b34124e8a3b, //depot/perl@3298
2693         # close directory handles properly when localized,
2694         # tweaked slightly by commit 1236053a2c722e2b,
2695         # add test case for change#3298
2696         #
2697         # The fix is the last part of:
2698         #
2699         # various fixes for clean build and test on win32; configpm broken,
2700         # needed to open myconfig.SH rather than myconfig; sundry adjustments
2701         # to bytecode stuff; tweaks to DYNAMIC_ENV_FETCH code to make it
2702         # work under win32; getenv_sv() changed to getenv_len() since SVs
2703         # aren't visible in the lower echelons; remove bogus exports from
2704         # config.sym; PERL_OBJECT-ness for C++ exception support; null out
2705         # IoDIRP in filter_del() or sv_free() will attempt to close it
2706         #
2707         # The changed code is modified subsequently by commit e0c198038146b7a4
2708         apply_commit('a6c403648ecd5cc7', 'toke.c');
2709     }
2710
2711     if ($major < 6 && $^O eq 'netbsd'
2712         && !extract_from_file('unixish.h',
2713                               qr/defined\(NSIG\).*defined\(__NetBSD__\)/)) {
2714         apply_patch(<<'EOPATCH')
2715 diff --git a/unixish.h b/unixish.h
2716 index 2a6cbcd..eab2de1 100644
2717 --- a/unixish.h
2718 +++ b/unixish.h
2719 @@ -89,7 +89,7 @@
2720   */
2721  /* #define ALTERNATE_SHEBANG "#!" / **/
2722  
2723 -#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
2724 +#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) || defined(__NetBSD__)
2725  # include <signal.h>
2726  #endif
2727  
2728 EOPATCH
2729     }
2730
2731     if (($major >= 7 || $major <= 9) && $^O eq 'openbsd'
2732         && `uname -m` eq "sparc64\n"
2733         # added in 2000 by commit cb434fcc98ac25f5:
2734         && extract_from_file('regexec.c',
2735                              qr!/\* No need to save/restore up to this paren \*/!)
2736         # re-indented in 2006 by commit 95b2444054382532:
2737         && extract_from_file('regexec.c', qr/^\t\tCURCUR cc;$/)) {
2738         # Need to work around a bug in (at least) OpenBSD's 4.6's sparc64 #
2739         # compiler ["gcc (GCC) 3.3.5 (propolice)"]. Between commits
2740         # 3ec562b0bffb8b8b (2002) and 1a4fad37125bac3e^ (2005) the darling thing
2741         # fails to compile any code for the statement cc.oldcc = PL_regcc;
2742         #
2743         # If you refactor the code to "fix" that, or force the issue using set
2744         # in the debugger, the stack smashing detection code fires on return
2745         # from S_regmatch(). Turns out that the compiler doesn't allocate any
2746         # (or at least enough) space for cc.
2747         #
2748         # Restore the "uninitialised" value for cc before function exit, and the
2749         # stack smashing code is placated.  "Fix" 3ec562b0bffb8b8b (which
2750         # changes the size of auto variables used elsewhere in S_regmatch), and
2751         # the crash is visible back to bc517b45fdfb539b (which also changes
2752         # buffer sizes). "Unfix" 1a4fad37125bac3e and the crash is visible until
2753         # 5b47454deb66294b.  Problem goes away if you compile with -O, or hack
2754         # the code as below.
2755         #
2756         # Hence this turns out to be a bug in (old) gcc. Not a security bug we
2757         # still need to fix.
2758         apply_patch(<<'EOPATCH');
2759 diff --git a/regexec.c b/regexec.c
2760 index 900b491..6251a0b 100644
2761 --- a/regexec.c
2762 +++ b/regexec.c
2763 @@ -2958,7 +2958,11 @@ S_regmatch(pTHX_ regnode *prog)
2764                                 I,I
2765   *******************************************************************/
2766         case CURLYX: {
2767 -               CURCUR cc;
2768 +           union {
2769 +               CURCUR hack_cc;
2770 +               char hack_buff[sizeof(CURCUR) + 1];
2771 +           } hack;
2772 +#define cc hack.hack_cc
2773                 CHECKPOINT cp = PL_savestack_ix;
2774                 /* No need to save/restore up to this paren */
2775                 I32 parenfloor = scan->flags;
2776 @@ -2983,6 +2987,7 @@ S_regmatch(pTHX_ regnode *prog)
2777                 n = regmatch(PREVOPER(next));   /* start on the WHILEM */
2778                 regcpblow(cp);
2779                 PL_regcc = cc.oldcc;
2780 +#undef cc
2781                 saySAME(n);
2782             }
2783             /* NOT REACHED */
2784 EOPATCH
2785 }
2786
2787     if ($major < 8 && $^O eq 'openbsd'
2788         && !extract_from_file('perl.h', qr/include <unistd\.h>/)) {
2789         # This is part of commit 3f270f98f9305540, applied at a slightly
2790         # different location in perl.h, where the context is stable back to
2791         # 5.000
2792         apply_patch(<<'EOPATCH');
2793 diff --git a/perl.h b/perl.h
2794 index 9418b52..b8b1a7c 100644
2795 --- a/perl.h
2796 +++ b/perl.h
2797 @@ -496,6 +496,10 @@ register struct op *Perl_op asm(stringify(OP_IN_REGISTER));
2798  #   include <sys/param.h>
2799  #endif
2800  
2801 +/* If this causes problems, set i_unistd=undef in the hint file.  */
2802 +#ifdef I_UNISTD
2803 +#   include <unistd.h>
2804 +#endif
2805  
2806  /* Use all the "standard" definitions? */
2807  #if defined(STANDARD_C) && defined(I_STDLIB)
2808 EOPATCH
2809     }
2810 }
2811
2812 sub patch_ext {
2813     if (-f 'ext/POSIX/Makefile.PL'
2814         && extract_from_file('ext/POSIX/Makefile.PL',
2815                              qr/Explicitly avoid including/)) {
2816         # commit 6695a346c41138df, which effectively reverts 170888cff5e2ffb7
2817
2818         # PERL5LIB is populated by make_ext.pl with paths to the modules we need
2819         # to run, don't override this with "../../lib" since that may not have
2820         # been populated yet in a parallel build.
2821         apply_commit('6695a346c41138df');
2822     }
2823
2824     if (-f 'ext/Hash/Util/Makefile.PL'
2825         && extract_from_file('ext/Hash/Util/Makefile.PL',
2826                              qr/\bDIR\b.*'FieldHash'/)) {
2827         # ext/Hash/Util/Makefile.PL should not recurse to FieldHash's Makefile.PL
2828         # *nix, VMS and Win32 all know how to (and have to) call the latter directly.
2829         # As is, targets in ext/Hash/Util/FieldHash get called twice, which may result
2830         # in race conditions, and certainly messes up make clean; make distclean;
2831         apply_commit('550428fe486b1888');
2832     }
2833
2834     if ($major < 8 && $^O eq 'darwin' && !-f 'ext/DynaLoader/dl_dyld.xs') {
2835         checkout_file('ext/DynaLoader/dl_dyld.xs', 'f556e5b971932902');
2836         apply_patch(<<'EOPATCH');
2837 diff -u a/ext/DynaLoader/dl_dyld.xs~ a/ext/DynaLoader/dl_dyld.xs
2838 --- a/ext/DynaLoader/dl_dyld.xs~        2011-10-11 21:41:27.000000000 +0100
2839 +++ b/ext/DynaLoader/dl_dyld.xs 2011-10-11 21:42:20.000000000 +0100
2840 @@ -41,6 +41,35 @@
2841  #include "perl.h"
2842  #include "XSUB.h"
2843  
2844 +#ifndef pTHX
2845 +#  define pTHX         void
2846 +#  define pTHX_
2847 +#endif
2848 +#ifndef aTHX
2849 +#  define aTHX
2850 +#  define aTHX_
2851 +#endif
2852 +#ifndef dTHX
2853 +#  define dTHXa(a)     extern int Perl___notused(void)
2854 +#  define dTHX         extern int Perl___notused(void)
2855 +#endif
2856 +
2857 +#ifndef Perl_form_nocontext
2858 +#  define Perl_form_nocontext form
2859 +#endif
2860 +
2861 +#ifndef Perl_warn_nocontext
2862 +#  define Perl_warn_nocontext warn
2863 +#endif
2864 +
2865 +#ifndef PTR2IV
2866 +#  define PTR2IV(p)    (IV)(p)
2867 +#endif
2868 +
2869 +#ifndef get_av
2870 +#  define get_av perl_get_av
2871 +#endif
2872 +
2873  #define DL_LOADONCEONLY
2874  
2875  #include "dlutils.c"   /* SaveError() etc      */
2876 @@ -185,7 +191,7 @@
2877      CODE:
2878      DLDEBUG(1,PerlIO_printf(Perl_debug_log, "dl_load_file(%s,%x):\n", filename,flags));
2879      if (flags & 0x01)
2880 -       Perl_warn(aTHX_ "Can't make loaded symbols global on this platform while loading %s",filename);
2881 +       Perl_warn_nocontext("Can't make loaded symbols global on this platform while loading %s",filename);
2882      RETVAL = dlopen(filename, mode) ;
2883      DLDEBUG(2,PerlIO_printf(Perl_debug_log, " libref=%x\n", RETVAL));
2884      ST(0) = sv_newmortal() ;
2885 EOPATCH
2886         if ($major < 4 && !extract_from_file('util.c', qr/^form/m)) {
2887             apply_patch(<<'EOPATCH');
2888 diff -u a/ext/DynaLoader/dl_dyld.xs~ a/ext/DynaLoader/dl_dyld.xs
2889 --- a/ext/DynaLoader/dl_dyld.xs~        2011-10-11 21:56:25.000000000 +0100
2890 +++ b/ext/DynaLoader/dl_dyld.xs 2011-10-11 22:00:00.000000000 +0100
2891 @@ -60,6 +60,18 @@
2892  #  define get_av perl_get_av
2893  #endif
2894  
2895 +static char *
2896 +form(char *pat, ...)
2897 +{
2898 +    char *retval;
2899 +    va_list args;
2900 +    va_start(args, pat);
2901 +    vasprintf(&retval, pat, &args);
2902 +    va_end(args);
2903 +    SAVEFREEPV(retval);
2904 +    return retval;
2905 +}
2906 +
2907  #define DL_LOADONCEONLY
2908  
2909  #include "dlutils.c"   /* SaveError() etc      */
2910 EOPATCH
2911         }
2912     }
2913
2914     if ($major < 10) {
2915         if (!extract_from_file('ext/DB_File/DB_File.xs',
2916                                qr!^#else /\* Berkeley DB Version > 2 \*/$!)) {
2917             # This DB_File.xs is really too old to patch up.
2918             # Skip DB_File, unless we're invoked with an explicit -Unoextensions
2919             if (!exists $defines{noextensions}) {
2920                 $defines{noextensions} = 'DB_File';
2921             } elsif (defined $defines{noextensions}) {
2922                 $defines{noextensions} .= ' DB_File';
2923             }
2924         } elsif (!extract_from_file('ext/DB_File/DB_File.xs',
2925                                     qr/^#ifdef AT_LEAST_DB_4_1$/)) {
2926             # This line is changed by commit 3245f0580c13b3ab
2927             my $line = extract_from_file('ext/DB_File/DB_File.xs',
2928                                          qr/^(        status = \(?RETVAL->dbp->open\)?\(RETVAL->dbp, name, NULL, RETVAL->type, $)/);
2929             apply_patch(<<"EOPATCH");
2930 diff --git a/ext/DB_File/DB_File.xs b/ext/DB_File/DB_File.xs
2931 index 489ba96..fba8ded 100644
2932 --- a/ext/DB_File/DB_File.xs
2933 +++ b/ext/DB_File/DB_File.xs
2934 \@\@ -183,4 +187,8 \@\@
2935  #endif
2936  
2937 +#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
2938 +#    define AT_LEAST_DB_4_1
2939 +#endif
2940 +
2941  /* map version 2 features & constants onto their version 1 equivalent */
2942  
2943 \@\@ -1334,7 +1419,12 \@\@ SV *   sv ;
2944  #endif
2945  
2946 +#ifdef AT_LEAST_DB_4_1
2947 +        status = (RETVAL->dbp->open)(RETVAL->dbp, NULL, name, NULL, RETVAL->type, 
2948 +                               Flags, mode) ; 
2949 +#else
2950  $line
2951                                 Flags, mode) ; 
2952 +#endif
2953         /* printf("open returned %d %s\\n", status, db_strerror(status)) ; */
2954  
2955 EOPATCH
2956         }
2957     }
2958
2959     if ($major < 10 and -f 'ext/IPC/SysV/SysV.xs') {
2960         edit_file('ext/IPC/SysV/SysV.xs', sub {
2961                       my $xs = shift;
2962                       my $fixed = <<'EOFIX';
2963
2964 #include <sys/types.h>
2965 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2966 #ifndef HAS_SEM
2967 #   include <sys/ipc.h>
2968 #endif
2969 #   ifdef HAS_MSG
2970 #       include <sys/msg.h>
2971 #   endif
2972 #   ifdef HAS_SHM
2973 #       if defined(PERL_SCO) || defined(PERL_ISC)
2974 #           include <sys/sysmacros.h>   /* SHMLBA */
2975 #       endif
2976 #      include <sys/shm.h>
2977 #      ifndef HAS_SHMAT_PROTOTYPE
2978            extern Shmat_t shmat (int, char *, int);
2979 #      endif
2980 #      if defined(HAS_SYSCONF) && defined(_SC_PAGESIZE)
2981 #          undef  SHMLBA /* not static: determined at boot time */
2982 #          define SHMLBA sysconf(_SC_PAGESIZE)
2983 #      elif defined(HAS_GETPAGESIZE)
2984 #          undef  SHMLBA /* not static: determined at boot time */
2985 #          define SHMLBA getpagesize()
2986 #      endif
2987 #   endif
2988 #endif
2989 EOFIX
2990                       $xs =~ s!
2991 #include <sys/types\.h>
2992 .*
2993 (#ifdef newCONSTSUB|/\* Required)!$fixed$1!ms;
2994                       return $xs;
2995                   });
2996     }
2997 }
2998
2999 # Local variables:
3000 # cperl-indent-level: 4
3001 # indent-tabs-mode: nil
3002 # End:
3003 #
3004 # ex: set ts=8 sts=4 sw=4 et: