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