This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
renumber SVpad_STATE and free a bit in SvFLAGS
[perl5.git] / Porting / bisect-runner.pl
CommitLineData
6a8dbfd7
NC
1#!/usr/bin/perl -w
2use strict;
3
390a69a9 4use Getopt::Long qw(:config bundling no_auto_abbrev);
77ae6092 5use Pod::Usage;
2526f4b8 6use Config;
6a8dbfd7 7
2526f4b8
NC
8my @targets
9 = qw(config.sh config.h miniperl lib/Config.pm Fcntl perl test_prep);
6a8dbfd7 10
e4516dd0
NC
11my $cpus;
12if (open my $fh, '<', '/proc/cpuinfo') {
13 while (<$fh>) {
14 ++$cpus if /^processor\s+:\s+\d+$/;
15 }
d64af352
NC
16} elsif (-x '/sbin/sysctl') {
17 $cpus = 1 + $1 if `/sbin/sysctl hw.ncpu` =~ /^hw\.ncpu: (\d+)$/;
da83cd31
NC
18} elsif (-x '/usr/bin/getconf') {
19 $cpus = 1 + $1 if `/usr/bin/getconf _NPROCESSORS_ONLN` =~ /^(\d+)$/;
e4516dd0
NC
20}
21
f4800c99
NC
22my %options =
23 (
e4516dd0 24 jobs => defined $cpus ? $cpus + 1 : 2,
f4800c99
NC
25 'expect-pass' => 1,
26 clean => 1, # mostly for debugging this
27 );
6a8dbfd7 28
390a69a9
NC
29my @paths = qw(/usr/local/lib64 /lib64 /usr/lib64);
30
31my %defines =
32 (
33 usedevel => '',
34 optimize => '-g',
35 cc => 'ccache gcc',
36 ld => 'gcc',
e4516dd0 37 (`uname -sm` eq "Linux x86_64\n" ? (libpth => \@paths) : ()),
390a69a9
NC
38 );
39
f4800c99
NC
40unless(GetOptions(\%options,
41 'target=s', 'jobs|j=i', 'expect-pass=i',
42 'expect-fail' => sub { $options{'expect-pass'} = 0; },
43 'clean!', 'one-liner|e=s', 'match=s', 'force-manifest',
77ae6092 44 'test-build', 'check-args', 'A=s@', 'usage|help|?',
390a69a9
NC
45 'D=s@' => sub {
46 my (undef, $val) = @_;
47 if ($val =~ /\A([^=]+)=(.*)/s) {
48 $defines{$1} = length $2 ? $2 : "\0";
49 } else {
50 $defines{$val} = '';
51 }
52 },
53 'U=s@' => sub {
54 $defines{$_[1]} = undef;
55 },
6a8dbfd7 56 )) {
77ae6092 57 pod2usage(exitval => 255, verbose => 1);
6a8dbfd7
NC
58}
59
f4800c99 60my ($target, $j, $match) = @options{qw(target jobs match)};
e295b7be 61
77ae6092
NC
62pod2usage(exitval => 255, verbose => 1) if $options{usage};
63pod2usage(exitval => 255, verbose => 1)
64 unless @ARGV || $match || $options{'test-build'} || defined $options{'one-liner'};
6a8dbfd7 65
f4800c99 66exit 0 if $options{'check-args'};
6a8dbfd7 67
77ae6092
NC
68=head1 NAME
69
70bisect.pl - use git bisect to pinpoint changes
71
72=head1 SYNOPSIS
73
74 # When did this become an error?
75 .../Porting/bisect.pl -e 'my $a := 2;'
71d80638 76 # When did this stop being an error?
77ae6092
NC
77 .../Porting/bisect.pl --expect-fail -e '1 // 2'
78 # When did this stop matching?
79 .../Porting/bisect.pl --match '\b(?:PL_)hash_seed_set\b'
80 # When did this start matching?
81 .../Porting/bisect.pl --expect-fail --match '\buseithreads\b'
82 # When did this test program stop working?
83 .../Porting/bisect.pl --target=perl -- ./perl -Ilib test_prog.pl
84 # When did this first become valid syntax?
85 .../Porting/bisect.pl --target=miniperl --end=v5.10.0 \
86 --expect-fail -e 'my $a := 2;'
87 # What was the last revision to build with these options?
88 .../Porting/bisect.pl --test-build -Dd_dosuid
89
90=head1 DESCRIPTION
91
92Together C<bisect.pl> and C<bisect-runner.pl> attempt to automate the use
93of C<git bisect> as much as possible. With one command (and no other files)
94it's easy to find out
95
96=over 4
97
98=item *
99
100Which commit caused this example code to break?
101
102=item *
103
104Which commit caused this example code to start working?
105
106=item *
107
108Which commit added the first to match this regex?
109
110=item *
111
112Which commit removed the last to match this regex?
113
114=back
115
116usually without needing to know which versions of perl to use as start and
117end revisions.
118
119By default C<bisect.pl> will process all options, then use the rest of the
120command line as arguments to list C<system> to run a test case. By default,
121the test case should pass (exit with 0) on earlier perls, and fail (exit
122non-zero) on I<blead>. C<bisect.pl> will use C<bisect-runner.pl> to find the
123earliest stable perl version on which the test case passes, check that it
124fails on blead, and then use C<bisect-runner.pl> with C<git bisect run> to
125find the commit which caused the failure.
126
127Because the test case is the complete argument to C<system>, it is easy to
128run something other than the F<perl> built, if necessary. If you need to run
129the perl built, you'll probably need to invoke it as C<./perl -Ilib ...>
130
5842706e
NC
131You need a clean checkout to run a bisect, and you can't use the checkout
132which contains F<Porting/bisect.pl> (because C<git bisect>) will check out
133a revision before F<Porting/bisect-runner.pl> was added, which
134C<git bisect run> needs). If your working checkout is called F<perl>, the
135simplest solution is to make a local clone, and run from that. I<i.e.>:
136
137 cd ..
138 git clone perl perl2
139 cd perl2
140 ../perl/Porting/bisect.pl ...
141
77ae6092
NC
142=head1 OPTIONS
143
144=over 4
145
146=item *
147
148--start I<commit-ish>
149
150Earliest revision to test, as a I<commit-ish> (a tag, commit or anything
71d80638 151else C<git> understands as a revision). If not specified, C<bisect.pl> will
77ae6092
NC
152search stable perl releases from 5.002 to 5.14.0 until it finds one where
153the test case passes.
154
155=item *
156
157--end I<commit-ish>
158
159Most recent revision to test, as a I<commit-ish>. If not specified, defaults
b4f0ec5f 160to I<blead>.
77ae6092
NC
161
162=item *
163
164--target I<target>
165
166F<Makefile> target (or equivalent) needed, to run the test case. If specified,
167this should be one of
168
169=over 4
170
171=item *
172
173I<config.sh>
174
175Just run C<Configure>
176
177=item *
178
179I<config.h>
180
181Run the various F<*.SH> files to generate F<Makefile>, F<config.h>, I<etc>.
182
183=item *
184
185I<miniperl>
186
187Build F<miniperl>.
188
189=item *
190
191I<lib/Config.pm>
192
193Use F<miniperl> to build F<lib/Config.pm>
194
195=item *
196
2526f4b8
NC
197I<Fcntl>
198
199Build F<lib/auto/Fcntl/Fnctl.so> (strictly, C<.$Config{so}>). As L<Fcntl>
200is simple XS module present since 5.000, this provides a fast test of
b4f0ec5f 201whether XS modules can be built. Note, XS modules are built by F<miniperl>,
2526f4b8
NC
202hence this target will not build F<perl>.
203
204=item *
205
77ae6092
NC
206I<perl>
207
208Build F<perl>. This also builds pure-Perl modules in F<cpan>, F<dist> and
2526f4b8
NC
209F<ext>. XS modules (such as L<Fcntl>) are not built.
210
211=item *
212
77ae6092
NC
213I<test_prep>
214
215Build everything needed to run the tests. This is the default if we're
216running test code, but is time consuming, as it means building all
b4f0ec5f 217XS modules. For older F<Makefile>s, the previous name of C<test-prep>
77ae6092
NC
218is automatically substituted. For very old F<Makefile>s, C<make test> is
219run, as there is no target provided to just get things ready, and for 5.004
220and earlier the tests run very quickly.
221
222=back
223
224=item *
225
226--one-liner 'code to run'
227
228=item *
229
230-e 'code to run'
231
a1756669 232Example code to run, just like you'd use with C<perl -e>.
77ae6092
NC
233
234This prepends C<./perl -Ilib -e 'code to run'> to the test case given,
b4f0ec5f 235or C<./miniperl> if I<target> is C<miniperl>.
77ae6092
NC
236
237(Usually you'll use C<-e> instead of providing a test case in the
238non-option arguments to C<bisect.pl>)
239
240C<-E> intentionally isn't supported, as it's an error in 5.8.0 and earlier,
241which interferes with detecting errors in the example code itself.
242
243=item *
244
245--expect-fail
246
247The test case should fail for the I<start> revision, and pass for the I<end>
248revision. The bisect run will find the first commit where it passes.
249
250=item *
251
252-Dusethreads
253
254=item *
255
256-Uusedevel
257
258=item *
259
260-Accflags=-DNO_MATHOMS
261
262Arguments to pass to F<Configure>. Repeated C<-A> arguments are passed
263through as is. C<-D> and C<-U> are processed in order, and override
264previous settings for the same parameter.
265
266=item *
267
b4f0ec5f 268--jobs I<jobs>
77ae6092
NC
269
270=item *
271
b4f0ec5f 272-j I<jobs>
77ae6092 273
da83cd31
NC
274Number of C<make> jobs to run in parallel. If F</proc/cpuinfo> exists and
275can be parsed, or F</sbin/sysctl> exists and reports C<hw.ncpu>, or
276F</usr/bin/getconf> exists and reports C<_NPROCESSORS_ONLN> defaults to 1 +
277I<number of CPUs>. Otherwise defaults to 2.
77ae6092
NC
278
279=item *
280
b4f0ec5f 281--match pattern
77ae6092
NC
282
283Instead of running a test program to determine I<pass> or I<fail>, pass
284if the given regex matches, and hence search for the commit that removes
285the last matching file.
286
287If no I<target> is specified, the match is against all files in the
288repository (which is fast). If a I<target> is specified, that target is
289built, and the match is against only the built files. C<--expect-fail> can
290be used with C<--match> to search for a commit that adds files that match.
291
292=item *
293
294--test-build
295
296Test that the build completes, without running any test case.
297
298By default, if the build for the desired I<target> fails to complete,
299F<bisect-runner.pl> reports a I<skip> back to C<git bisect>, the assumption
300being that one wants to find a commit which changed state "builds && passes"
301to "builds && fails". If instead one is interested in which commit broke the
302build (possibly for particular F<Configure> options), use I<--test-build>
303to treat a build failure as a failure, not a "skip".
304
b4f0ec5f
NC
305Often this option isn't as useful as it first seems, because I<any> build
306failure will be reported to C<git bisect> as a failure, not just the failure
307that you're interested in. Generally, to debug a particular problem, it's
308more useful to use a I<target> that builds properly at the point of interest,
309and then a test case that runs C<make>. For example:
310
311 .../Porting/bisect.pl --start=perl-5.000 --end=perl-5.002 \
312 --expect-fail --force-manifest --target=miniperl make perl
313
314will find the first revision capable of building C<DynaLoader> and then
315C<perl>, without becoming confused by revisions where C<miniperl> won't
316even link.
317
77ae6092
NC
318=item *
319
b4f0ec5f
NC
320--force-manifest
321
77ae6092
NC
322By default, a build will "skip" if any files listed in F<MANIFEST> are not
323present. Usually this is useful, as it avoids false-failures. However, there
324are some long ranges of commits where listed files are missing, which can
325cause a bisect to abort because all that remain are skipped revisions.
326
327In these cases, particularly if the test case uses F<miniperl> and no modules,
328it may be more useful to force the build to continue, even if files
329F<MANIFEST> are missing.
330
331=item *
332
333--expect-pass [0|1]
334
335C<--expect-pass=0> is equivalent to C<--expect-fail>. I<1> is the default.
336
337=item *
338
339--no-clean
340
341Tell F<bisect-runner.pl> not to clean up after the build. This allows one
342to use F<bisect-runner.pl> to build the current particular perl revision for
343interactive testing, or for debugging F<bisect-runner.pl>.
344
345Passing this to F<bisect.pl> will likely cause the bisect to fail badly.
346
347=item *
348
349--check-args
350
351Validate the options and arguments, and exit silently if they are valid.
352
353=item *
354
355--usage
356
357=item *
358
359--help
360
361=item *
362
363-?
364
365Display the usage information and exit.
366
367=back
368
369=cut
370
0afef97d 371die "$0: Can't build $target" if defined $target && !grep {@targets} $target;
6a8dbfd7
NC
372
373$j = "-j$j" if $j =~ /\A\d+\z/;
374
0142f0ce
NC
375# Sadly, however hard we try, I don't think that it will be possible to build
376# modules in ext/ on x86_64 Linux before commit e1666bf5602ae794 on 1999/12/29,
377# which updated to MakeMaker 3.7, which changed from using a hard coded ld
378# in the Makefile to $(LD). On x86_64 Linux the "linker" is gcc.
379
6a8dbfd7
NC
380sub extract_from_file {
381 my ($file, $rx, $default) = @_;
382 open my $fh, '<', $file or die "Can't open $file: $!";
383 while (<$fh>) {
384 my @got = $_ =~ $rx;
385 return wantarray ? @got : $got[0]
386 if @got;
387 }
388 return $default if defined $default;
389 return;
390}
391
c59e8fd6
NC
392sub edit_file {
393 my ($file, $munger) = @_;
394 local $/;
395 open my $fh, '<', $file or die "Can't open $file: $!";
396 my $orig = <$fh>;
397 die "Can't read $file: $!" unless defined $orig && close $fh;
398 my $new = $munger->($orig);
399 return if $new eq $orig;
400 open $fh, '>', $file or die "Can't open $file: $!";
401 print $fh $new or die "Can't print to $file: $!";
402 close $fh or die "Can't close $file: $!";
403}
404
405sub apply_patch {
406 my $patch = shift;
407
408 my ($file) = $patch =~ qr!^diff.*a/(\S+) b/\1!;
409 open my $fh, '|-', 'patch', '-p1' or die "Can't run patch: $!";
410 print $fh $patch;
411 close $fh or die "Can't patch $file: $?, $!";
412}
413
ab4a15f9 414sub clean {
f4800c99 415 if ($options{clean}) {
ab4a15f9
NC
416 # Needed, because files that are build products in this checked out
417 # version might be in git in the next desired version.
9da8cb0a 418 system 'git clean -dxf </dev/null';
ab4a15f9
NC
419 # Needed, because at some revisions the build alters checked out files.
420 # (eg pod/perlapi.pod). Also undoes any changes to makedepend.SH
9da8cb0a 421 system 'git reset --hard HEAD </dev/null';
ab4a15f9
NC
422 }
423}
424
425sub skip {
426 my $reason = shift;
427 clean();
428 warn "skipping - $reason";
429 exit 125;
430}
431
f1050811
NC
432sub report_and_exit {
433 my ($ret, $pass, $fail, $desc) = @_;
434
435 clean();
436
f4800c99 437 my $got = ($options{'expect-pass'} ? !$ret : $ret) ? 'good' : 'bad';
f1050811
NC
438 if ($ret) {
439 print "$got - $fail $desc\n";
440 } else {
441 print "$got - $pass $desc\n";
442 }
443
444 exit($got eq 'bad');
445}
446
0afef97d
NC
447sub match_and_exit {
448 my $target = shift;
449 my $matches = 0;
450 my $re = qr/$match/;
451 my @files;
452
453 {
454 local $/ = "\0";
455 @files = defined $target ? `git ls-files -o -z`: `git ls-files -z`;
456 chomp @files;
457 }
458
459 foreach my $file (@files) {
460 open my $fh, '<', $file or die "Can't open $file: $!";
461 while (<$fh>) {
462 if ($_ =~ $re) {
463 ++$matches;
464 if (tr/\t\r\n -~\200-\377//c) {
465 print "Binary file $file matches\n";
466 } else {
467 $_ .= "\n" unless /\n\z/;
468 print "$file: $_";
469 }
470 }
471 }
472 close $fh or die "Can't close $file: $!";
473 }
474 report_and_exit(!$matches,
475 $matches == 1 ? '1 match for' : "$matches matches for",
476 'no matches for', $match);
477}
478
6a8dbfd7 479# Not going to assume that system perl is yet new enough to have autodie
9da8cb0a 480system 'git clean -dxf </dev/null' and die;
6a8dbfd7 481
0afef97d
NC
482if (!defined $target) {
483 match_and_exit() if $match;
484 $target = 'test_prep';
bc96a05a
NC
485}
486
4b081584
NC
487skip('no Configure - is this the //depot/perlext/Compiler branch?')
488 unless -f 'Configure';
489
dbcdc176
NC
490# This changes to PERL_VERSION in 4d8076ea25903dcb in 1999
491my $major
492 = extract_from_file('patchlevel.h',
493 qr/^#define\s+(?:PERL_VERSION|PATCHLEVEL)\s+(\d+)\s/,
494 0);
495
0142f0ce
NC
496if ($major < 1) {
497 if (extract_from_file('Configure',
498 qr/^ \*=\*\) echo "\$1" >> \$optdef;;$/)) {
499 # This is " Spaces now allowed in -D command line options.",
500 # part of commit ecfc54246c2a6f42
501 apply_patch(<<'EOPATCH');
502diff --git a/Configure b/Configure
503index 3d3b38d..78ffe16 100755
504--- a/Configure
505+++ b/Configure
506@@ -652,7 +777,8 @@ while test $# -gt 0; do
507 echo "$me: use '-U symbol=', not '-D symbol='." >&2
508 echo "$me: ignoring -D $1" >&2
509 ;;
510- *=*) echo "$1" >> $optdef;;
511+ *=*) echo "$1" | \
512+ sed -e "s/'/'\"'\"'/g" -e "s/=\(.*\)/='\1'/" >> $optdef;;
513 *) echo "$1='define'" >> $optdef;;
514 esac
515 shift
516EOPATCH
517 }
518 if (extract_from_file('Configure', qr/^if \$contains 'd_namlen' \$xinc\b/)) {
519 # Configure's original simple "grep" for d_namlen falls foul of the
520 # approach taken by the glibc headers:
521 # #ifdef _DIRENT_HAVE_D_NAMLEN
522 # # define _D_EXACT_NAMLEN(d) ((d)->d_namlen)
523 #
524 # where _DIRENT_HAVE_D_NAMLEN is not defined on Linux.
525 # This is also part of commit ecfc54246c2a6f42
526 apply_patch(<<'EOPATCH');
527diff --git a/Configure b/Configure
528index 3d3b38d..78ffe16 100755
529--- a/Configure
530+++ b/Configure
531@@ -3935,7 +4045,8 @@ $rm -f try.c
532
533 : see if the directory entry stores field length
534 echo " "
535-if $contains 'd_namlen' $xinc >/dev/null 2>&1; then
536+$cppstdin $cppflags $cppminus < "$xinc" > try.c
537+if $contains 'd_namlen' try.c >/dev/null 2>&1; then
538 echo "Good, your directory entry keeps length information in d_namlen." >&4
539 val="$define"
540 else
541EOPATCH
542 }
543}
d90ae42b 544
7c22f158
NC
545if ($major < 5 && extract_from_file('Configure',
546 qr/^if test ! -t 0; then$/)) {
547 # Before dfe9444ca7881e71, Configure would refuse to run if stdin was not a
548 # tty. With that commit, the tty requirement was dropped for -de and -dE
549 # For those older versions, it's probably easiest if we simply remove the
550 # sanity test.
551 apply_patch(<<'EOPATCH');
552diff --git a/Configure b/Configure
553index 0071a7c..8a61caa 100755
554--- a/Configure
555+++ b/Configure
556@@ -93,7 +93,2 @@ esac
557
558-: Sanity checks
559-if test ! -t 0; then
560- echo "Say 'sh $me', not 'sh <$me'"
561- exit 1
562-fi
563
564EOPATCH
565}
566
d90ae42b 567if ($major < 10 && extract_from_file('Configure', qr/^set malloc\.h i_malloc$/)) {
5541f5e3
NC
568 # This is commit 01d07975f7ef0e7d, trimmed, with $compile inlined as
569 # prior to bd9b35c97ad661cc Configure had the malloc.h test before the
570 # definition of $compile.
d90ae42b
NC
571 apply_patch(<<'EOPATCH');
572diff --git a/Configure b/Configure
573index 3d2e8b9..6ce7766 100755
574--- a/Configure
575+++ b/Configure
576@@ -6743,5 +6743,22 @@ set d_dosuid
577
578 : see if this is a malloc.h system
579-set malloc.h i_malloc
580-eval $inhdr
581+: we want a real compile instead of Inhdr because some systems have a
582+: malloc.h that just gives a compile error saying to use stdlib.h instead
583+echo " "
584+$cat >try.c <<EOCP
585+#include <stdlib.h>
586+#include <malloc.h>
587+int main () { return 0; }
588+EOCP
589+set try
5541f5e3 590+if $cc $optimize $ccflags $ldflags -o try $* try.c $libs > /dev/null 2>&1; then
d90ae42b
NC
591+ echo "<malloc.h> found." >&4
592+ val="$define"
593+else
594+ echo "<malloc.h> NOT found." >&4
595+ val="$undef"
596+fi
597+$rm -f try.c try
598+set i_malloc
599+eval $setvar
600
601EOPATCH
602}
a1756669 603
6a8dbfd7
NC
604# There was a bug in makedepend.SH which was fixed in version 96a8704c.
605# Symptom was './makedepend: 1: Syntax error: Unterminated quoted string'
606# Remove this if you're actually bisecting a problem related to makedepend.SH
9da8cb0a 607system 'git show blead:makedepend.SH > makedepend.SH </dev/null' and die;
6a8dbfd7 608
d64af352
NC
609if ($^O eq 'freebsd') {
610 # There are rather too many version-specific FreeBSD hints fixes to patch
611 # individually. Also, more than once the FreeBSD hints file has been
612 # written in what turned out to be a rather non-future-proof style,
613 # with case statements treating the most recent version as the exception,
614 # instead of treating previous versions' behaviour explicitly and changing
615 # the default to cater for the current behaviour. (As strangely, future
616 # versions inherit the current behaviour.)
9da8cb0a
NC
617 system 'git show blead:hints/freebsd.sh > hints/freebsd.sh </dev/null'
618 and die;
641e2ba6
NC
619
620 if ($major < 2) {
621 # 5.002 Configure and later have code to
622 #
623 # : Try to guess additional flags to pick up local libraries.
624 #
625 # which will automatically add --L/usr/local/lib because libpth
626 # contains /usr/local/lib
627 #
628 # Without it, if Configure finds libraries in /usr/local/lib (eg
629 # libgdbm.so) and adds them to the compiler commandline (as -lgdbm),
630 # then the link will fail. We can't fix this up in config.sh because
631 # the link will *also* fail in the test compiles that Configure does
632 # (eg $inlibc) which makes Configure get all sorts of things
633 # wrong. :-( So bodge it here.
634 #
635 # Possibly other platforms will need something similar. (if they
636 # have "wanted" libraries in /usr/local/lib, but the compiler
637 # doesn't default to putting that directory in its link path)
638 apply_patch(<<'EOPATCH');
639--- perl2/hints/freebsd.sh.orig 2011-10-05 16:44:55.000000000 +0200
640+++ perl2/hints/freebsd.sh 2011-10-05 16:45:52.000000000 +0200
641@@ -125,7 +125,7 @@
642 else
643 libpth="/usr/lib /usr/local/lib"
644 glibpth="/usr/lib /usr/local/lib"
645- ldflags="-Wl,-E "
646+ ldflags="-Wl,-E -L/usr/local/lib "
647 lddlflags="-shared "
648 fi
649 cccdlflags='-DPIC -fPIC'
650@@ -133,7 +133,7 @@
651 *)
652 libpth="/usr/lib /usr/local/lib"
653 glibpth="/usr/lib /usr/local/lib"
654- ldflags="-Wl,-E "
655+ ldflags="-Wl,-E -L/usr/local/lib "
656 lddlflags="-shared "
657 cccdlflags='-DPIC -fPIC'
658 ;;
641e2ba6
NC
659EOPATCH
660 }
d64af352
NC
661}
662
6a8dbfd7
NC
663# if Encode is not needed for the test, you can speed up the bisect by
664# excluding it from the runs with -Dnoextensions=Encode
665# ccache is an easy win. Remove it if it causes problems.
6a8dbfd7
NC
666# Commit 1cfa4ec74d4933da adds ignore_versioned_solibs to Configure, and sets it
667# to true in hints/linux.sh
668# On dromedary, from that point on, Configure (by default) fails to find any
669# libraries, because it scans /usr/local/lib /lib /usr/lib, which only contain
670# versioned libraries. Without -lm, the build fails.
671# Telling /usr/local/lib64 /lib64 /usr/lib64 works from that commit onwards,
672# until commit faae14e6e968e1c0 adds it to the hints.
673# However, prior to 1cfa4ec74d4933da telling Configure the truth doesn't work,
674# because it will spot versioned libraries, pass them to the compiler, and then
675# bail out pretty early on. Configure won't let us override libswanted, but it
676# will let us override the entire libs list.
677
678unless (extract_from_file('Configure', 'ignore_versioned_solibs')) {
679 # Before 1cfa4ec74d4933da, so force the libs list.
680
681 my @libs;
682 # This is the current libswanted list from Configure, less the libs removed
683 # by current hints/linux.sh
684 foreach my $lib (qw(sfio socket inet nsl nm ndbm gdbm dbm db malloc dl dld
685 ld sun m crypt sec util c cposix posix ucb BSD)) {
686 foreach my $dir (@paths) {
687 next unless -f "$dir/lib$lib.so";
688 push @libs, "-l$lib";
689 last;
690 }
691 }
390a69a9 692 $defines{libs} = \@libs unless exists $defines{libs};
6a8dbfd7
NC
693}
694
4b081584
NC
695# This seems to be necessary to avoid makedepend becoming confused, and hanging
696# on stdin. Seems that the code after make shlist || ...here... is never run.
390a69a9
NC
697$defines{trnl} = q{'\n'}
698 if $major < 4 && !exists $defines{trnl};
4b081584 699
390a69a9
NC
700$defines{usenm} = undef
701 if $major < 2 && !exists $defines{usenm};
0142f0ce 702
67382a3b
NC
703my (@missing, @created_dirs);
704
f4800c99 705if ($options{'force-manifest'}) {
67382a3b
NC
706 open my $fh, '<', 'MANIFEST'
707 or die "Could not open MANIFEST: $!";
708 while (<$fh>) {
709 next unless /^(\S+)/;
710 push @missing, $1
711 unless -f $1;
712 }
713 close $fh or die "Can't close MANIFEST: $!";
714
715 foreach my $pathname (@missing) {
716 my @parts = split '/', $pathname;
717 my $leaf = pop @parts;
718 my $path = '.';
719 while (@parts) {
720 $path .= '/' . shift @parts;
721 next if -d $path;
722 mkdir $path, 0700 or die "Can't create $path: $!";
723 unshift @created_dirs, $path;
724 }
725 open $fh, '>', $pathname or die "Can't open $pathname: $!";
726 close $fh or die "Can't close $pathname: $!";
727 chmod 0, $pathname or die "Can't chmod 0 $pathname: $!";
728 }
729}
730
dd4e46d7 731my @ARGS = $target eq 'config.sh' ? '-dEs' : '-des';
390a69a9
NC
732foreach my $key (sort keys %defines) {
733 my $val = $defines{$key};
734 if (ref $val) {
735 push @ARGS, "-D$key=@$val";
736 } elsif (!defined $val) {
737 push @ARGS, "-U$key";
738 } elsif (!length $val) {
739 push @ARGS, "-D$key";
740 } else {
741 $val = "" if $val eq "\0";
742 push @ARGS, "-D$key=$val";
743 }
744}
745push @ARGS, map {"-A$_"} @{$options{A}};
746
6a8dbfd7
NC
747# </dev/null because it seems that some earlier versions of Configure can
748# call commands in a way that now has them reading from stdin (and hanging)
749my $pid = fork;
750die "Can't fork: $!" unless defined $pid;
751if (!$pid) {
7c22f158
NC
752 open STDIN, '<', '/dev/null';
753 # If a file in MANIFEST is missing, Configure asks if you want to
754 # continue (the default being 'n'). With stdin closed or /dev/null,
755 # it exits immediately and the check for config.sh below will skip.
6a8dbfd7
NC
756 exec './Configure', @ARGS;
757 die "Failed to start Configure: $!";
758}
759waitpid $pid, 0
760 or die "wait for Configure, pid $pid failed: $!";
761
0afef97d
NC
762if ($target =~ /config\.s?h/) {
763 match_and_exit($target) if $match && -f $target;
dd4e46d7
NC
764 report_and_exit(!-f $target, 'could build', 'could not build', $target);
765} elsif (!-f 'config.sh') {
766 # Skip if something went wrong with Configure
767
768 skip('could not build config.sh');
769}
6a8dbfd7 770
67382a3b
NC
771# This is probably way too paranoid:
772if (@missing) {
773 my @errors;
eb4906f4 774 require Fcntl;
67382a3b
NC
775 foreach my $file (@missing) {
776 my (undef, undef, $mode, undef, undef, undef, undef, $size)
777 = stat $file;
778 if (!defined $mode) {
779 push @errors, "Added file $file has been deleted by Configure";
780 next;
781 }
eb4906f4 782 if (Fcntl::S_IMODE($mode) != 0) {
67382a3b
NC
783 push @errors,
784 sprintf 'Added file %s had mode changed by Configure to %03o',
785 $file, $mode;
786 }
787 if ($size != 0) {
788 push @errors,
789 "Added file $file had sized changed by Configure to $size";
790 }
791 unlink $file or die "Can't unlink $file: $!";
792 }
793 foreach my $dir (@created_dirs) {
794 rmdir $dir or die "Can't rmdir $dir: $!";
795 }
6c0925a0
NC
796 skip("@errors")
797 if @errors;
67382a3b
NC
798}
799
6a8dbfd7
NC
800# Correct makefile for newer GNU gcc
801# Only really needed if you comment out the use of blead's makedepend.SH
802{
803 local $^I = "";
804 local @ARGV = qw(makefile x2p/makefile);
805 while (<>) {
806 print unless /<(?:built-in|command|stdin)/;
807 }
808}
6a8dbfd7 809
0142f0ce
NC
810if ($major == 2 && extract_from_file('perl.c', qr/^ fclose\(e_fp\);$/)) {
811 # need to patch perl.c to avoid calling fclose() twice on e_fp when using -e
812 # This diff is part of commit ab821d7fdc14a438. The second close was
813 # introduced with perl-5.002, commit a5f75d667838e8e7
814 # Might want a6c477ed8d4864e6 too, for the corresponding change to pp_ctl.c
815 # (likely without this, eval will have "fun")
816 apply_patch(<<'EOPATCH');
817diff --git a/perl.c b/perl.c
818index 03c4d48..3c814a2 100644
819--- a/perl.c
820+++ b/perl.c
821@@ -252,6 +252,7 @@ setuid perl scripts securely.\n");
822 #ifndef VMS /* VMS doesn't have environ array */
823 origenviron = environ;
824 #endif
825+ e_tmpname = Nullch;
826
827 if (do_undump) {
828
829@@ -405,6 +406,7 @@ setuid perl scripts securely.\n");
830 if (e_fp) {
831 if (Fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
832 croak("Can't write to temp file for -e: %s", Strerror(errno));
833+ e_fp = Nullfp;
834 argc++,argv--;
835 scriptname = e_tmpname;
836 }
837@@ -470,10 +472,10 @@ setuid perl scripts securely.\n");
838 curcop->cop_line = 0;
839 curstash = defstash;
840 preprocess = FALSE;
841- if (e_fp) {
842- fclose(e_fp);
843- e_fp = Nullfp;
844+ if (e_tmpname) {
845 (void)UNLINK(e_tmpname);
846+ Safefree(e_tmpname);
847+ e_tmpname = Nullch;
848 }
849
850 /* now that script is parsed, we can modify record separator */
851@@ -1369,7 +1371,7 @@ SV *sv;
852 scriptname = xfound;
853 }
854
855- origfilename = savepv(e_fp ? "-e" : scriptname);
856+ origfilename = savepv(e_tmpname ? "-e" : scriptname);
857 curcop->cop_filegv = gv_fetchfile(origfilename);
858 if (strEQ(origfilename,"-"))
859 scriptname = "";
860
861EOPATCH
862}
863
c59e8fd6
NC
864if ($major < 10 and -f 'ext/IPC/SysV/SysV.xs') {
865 edit_file('ext/IPC/SysV/SysV.xs', sub {
866 my $xs = shift;
867 my $fixed = <<'EOFIX';
868
869#include <sys/types.h>
870#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
871#ifndef HAS_SEM
872# include <sys/ipc.h>
873#endif
874# ifdef HAS_MSG
875# include <sys/msg.h>
876# endif
877# ifdef HAS_SHM
878# if defined(PERL_SCO) || defined(PERL_ISC)
879# include <sys/sysmacros.h> /* SHMLBA */
880# endif
881# include <sys/shm.h>
882# ifndef HAS_SHMAT_PROTOTYPE
883 extern Shmat_t shmat (int, char *, int);
884# endif
885# if defined(HAS_SYSCONF) && defined(_SC_PAGESIZE)
886# undef SHMLBA /* not static: determined at boot time */
887# define SHMLBA sysconf(_SC_PAGESIZE)
888# elif defined(HAS_GETPAGESIZE)
889# undef SHMLBA /* not static: determined at boot time */
890# define SHMLBA getpagesize()
891# endif
892# endif
893#endif
894EOFIX
895 $xs =~ s!
896#include <sys/types\.h>
897.*
898(#ifdef newCONSTSUB|/\* Required)!$fixed$1!ms;
899 return $xs;
900 });
901}
902
9a999a97 903# Parallel build for miniperl is safe
9da8cb0a 904system "make $j miniperl </dev/null";
9a999a97 905
2526f4b8
NC
906my $expected = $target =~ /^test/ ? 't/perl'
907 : $target eq 'Fcntl' ? "lib/auto/Fcntl/Fcntl.$Config{so}"
908 : $target;
909my $real_target = $target eq 'Fcntl' ? $expected : $target;
910
9a999a97
NC
911if ($target ne 'miniperl') {
912 # Nearly all parallel build issues fixed by 5.10.0. Untrustworthy before that.
372ba1f9 913 $j = '' if $major < 10;
9a999a97 914
2526f4b8 915 if ($real_target eq 'test_prep') {
9a999a97
NC
916 if ($major < 8) {
917 # test-prep was added in 5.004_01, 3e3baf6d63945cb6.
918 # renamed to test_prep in 2001 in 5fe84fd29acaf55c.
919 # earlier than that, just make test. It will be fast enough.
2526f4b8
NC
920 $real_target = extract_from_file('Makefile.SH',
921 qr/^(test[-_]prep):/,
922 'test');
9a999a97 923 }
6a8dbfd7 924 }
6a8dbfd7 925
9da8cb0a 926 system "make $j $real_target </dev/null";
9a999a97 927}
6a8dbfd7 928
67382a3b
NC
929my $missing_target = $expected =~ /perl$/ ? !-x $expected : !-r $expected;
930
f4800c99 931if ($options{'test-build'}) {
2526f4b8
NC
932 report_and_exit($missing_target, 'could build', 'could not build',
933 $real_target);
67382a3b 934} elsif ($missing_target) {
2526f4b8 935 skip("could not build $real_target");
67382a3b 936}
6a8dbfd7 937
2526f4b8 938match_and_exit($real_target) if $match;
0afef97d
NC
939
940if (defined $options{'one-liner'}) {
2526f4b8 941 my $exe = $target =~ /^(?:perl$|test)/ ? 'perl' : 'miniperl';
0afef97d
NC
942 unshift @ARGV, "./$exe", '-Ilib', '-e', $options{'one-liner'};
943}
944
6a8dbfd7
NC
945# This is what we came here to run:
946my $ret = system @ARGV;
947
f1050811 948report_and_exit($ret, 'zero exit from', 'non-zero exit from', "@ARGV");
9a999a97
NC
949
950# Local variables:
951# cperl-indent-level: 4
952# indent-tabs-mode: nil
953# End:
954#
955# ex: set ts=8 sts=4 sw=4 et: