This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #127494] TODO test for $AUTOLOAD being set for DESTROY
[perl5.git] / Porting / bisect.pl
CommitLineData
6a8dbfd7
NC
1#!/usr/bin/perl -w
2use strict;
3
9b404864
NC
4=for comment
5
6Documentation for this is in bisect-runner.pl
7
8=cut
9
e295b7be
NC
10# The default, auto_abbrev will treat -e as an abbreviation of --end
11# Which isn't what we want.
12use Getopt::Long qw(:config pass_through no_auto_abbrev);
fbaf6793
MH
13use File::Spec;
14use File::Path qw(mkpath);
15
16my ($start, $end, $validate, $usage, $bad, $jobs, $make, $gold,
17 $module, $with_module);
18
19my $need_cpan_config;
c5134773 20my $cpan_config_dir;
6a8dbfd7 21
d3232d34 22$bad = !GetOptions('start=s' => \$start, 'end=s' => \$end,
02b83d1d 23 'jobs|j=i' => \$jobs, 'make=s' => \$make, 'gold=s' => \$gold,
fbaf6793 24 validate => \$validate, 'usage|help|?' => \$usage,
c5134773
MH
25 'module=s' => \$module, 'with-module=s' => \$with_module,
26 'cpan-config-dir=s' => \$cpan_config_dir);
0ed3b851 27unshift @ARGV, '--help' if $bad || $usage;
b826a648 28unshift @ARGV, '--validate' if $validate;
e2760528 29
fbaf6793 30if ($module || $with_module) {
fbaf6793
MH
31 unshift @ARGV, '--module', $module if defined $module;
32 unshift @ARGV, '--with-module', $with_module if defined $with_module;
c5134773
MH
33
34 if ($cpan_config_dir) {
35 my $c = File::Spec->catfile($cpan_config_dir, 'CPAN', 'MyConfig.pm');
36 die "--cpan-config-dir: $c does not exist\n" unless -e $c;
37
38 unshift @ARGV, '--cpan-config-dir', $cpan_config_dir;
39 } else {
40 $need_cpan_config = 1;
41 }
fbaf6793
MH
42}
43
77ae6092
NC
44my $runner = $0;
45$runner =~ s/bisect\.pl/bisect-runner.pl/;
46
47die "Can't find bisect runner $runner" unless -f $runner;
48
0ed3b851
NC
49system $^X, $runner, '--check-args', '--check-shebang', @ARGV and exit 255;
50exit 255 if $bad;
51exit 0 if $usage;
52
0ed3b851 53my $start_time = time;
6a8dbfd7 54
d3232d34
NC
55if (!defined $jobs &&
56 !($^O eq 'hpux' && system((defined $make ? $make : 'make')
57 . ' --version >/dev/null 2>&1'))) {
0bc550ba
NC
58 # Try to default to (ab)use all the CPUs:
59 my $cpus;
60 if (open my $fh, '<', '/proc/cpuinfo') {
61 while (<$fh>) {
62 ++$cpus if /^processor\s+:\s+\d+$/;
63 }
64 } elsif (-x '/sbin/sysctl') {
65 $cpus = $1 if `/sbin/sysctl hw.ncpu` =~ /^hw\.ncpu: (\d+)$/;
66 } elsif (-x '/usr/bin/getconf') {
67 $cpus = $1 if `/usr/bin/getconf _NPROCESSORS_ONLN` =~ /^(\d+)$/;
68 }
69 $jobs = defined $cpus ? $cpus + 1 : 2;
70}
71
d3232d34
NC
72unshift @ARGV, '--jobs', $jobs if defined $jobs;
73unshift @ARGV, '--make', $make if defined $make;
0bc550ba 74
fbaf6793
MH
75if ($need_cpan_config) {
76 # Make sure we have a CPAN::MyConfig so if we start at an old
77 # revision CPAN doesn't ask for user input to configure itself
78
79 my $cdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
80 my $cfile = File::Spec->catfile($cdir, "MyConfig.pm");
81
82 unless (-e $cfile) {
83 printf <<EOF;
84I could not find a CPAN::MyConfig. We need to create one now so that
85you can bisect with --module or --with-module. I'll boot up the CPAN
86shell for you. Feel free to use defaults or change things as needed.
c5134773
MH
87We recommend using 'manual' over 'local::lib' if it asks.
88
fbaf6793 89Type 'quit' when finished.
c5134773 90
fbaf6793
MH
91EOF
92 system("$^X -MCPAN -e shell");
fbaf6793
MH
93 }
94}
95
4819dd2a 96# We try these in this order for the start revision if none is specified.
f66c64b1
NC
97my @stable = map {chomp $_; $_} grep {/v5\.[0-9]+[02468]\.0$/} `git tag -l`;
98die "git tag -l didn't seem to return any tags for stable releases"
99 unless @stable;
100unshift @stable, qw(perl-5.005 perl-5.6.0 perl-5.8.0);
3ffe2687
NC
101
102{
103 my ($dev_C, $ino_C) = stat 'Configure';
104 my ($dev_c, $ino_c) = stat 'configure';
105 if (defined $dev_C && defined $dev_c
106 && $dev_C == $dev_c && $ino_C == $ino_c) {
eef6290d 107 print "You seem to be on a case-insensitive file system.\n\n";
3ffe2687
NC
108 } else {
109 unshift @stable, qw(perl-5.002 perl-5.003 perl-5.004)
110 }
111}
e295b7be 112
02b83d1d
NC
113unshift @ARGV, '--gold', defined $gold ? $gold : $stable[-1];
114
7f39e2df
NC
115if (!defined $end) {
116 # If we have a branch blead, use that as the end
117 $end = `git rev-parse --verify --quiet blead`;
118 die unless defined $end;
119 if (!length $end) {
120 # Else use whichever is newer - HEAD, or the most recent stable tag.
121 if (`git rev-list -n1 HEAD ^$stable[-1]` eq "") {
122 $end = pop @stable;
123 } else {
124 $end = 'HEAD';
125 }
126 }
127}
c382b335
NC
128
129# Canonicalising branches to revisions before moving the checkout permits one
130# to use revisions such as 'HEAD' for --start or --end
131foreach ($start, $end) {
132 next unless $_;
133 $_ = `git rev-parse $_`;
134 die unless defined $_;
135 chomp;
136}
6a8dbfd7 137
c8fde7fa
NC
138{
139 my $modified = my @modified = `git ls-files --modified --deleted --others`;
6a8dbfd7 140
c8fde7fa
NC
141 my ($dev0, $ino0) = stat $0;
142 die "Can't stat $0: $!" unless defined $ino0;
143 my ($dev1, $ino1) = stat 'Porting/bisect.pl';
144
145 my $inplace = defined $dev1 && $dev0 == $dev1 && $ino0 == $ino1;
146
147 if ($modified) {
148 my $final = $inplace
149 ? "Can't run a bisect using a dirty directory containing $runner"
150 : "You can use 'git clean -Xdf' to cleanup the ignored files";
151
152 die "This checkout is not clean, found file(s):\n",
153 join("\t","",@modified),
154 "$modified modified, untracked, or other file(s)\n",
155 "These files may not show in git status as they may be ignored.\n",
156 "$final.\n";
157 }
158
159 if ($inplace) {
160 # We assume that it's safe to copy the runner to the temporary
161 # directory and run it from there, because a shared /tmp should be +t
162 # and hence others are not be able to delete or rename our file.
163 require File::Temp;
164 my ($to, $toname) = File::Temp::tempfile();
165 die "Can't create tempfile"
166 unless $to;
167 open my $from, '<', $runner
168 or die "Can't open '$runner': $!";
169 local $/;
170 print {$to} <$from>
171 or die "Can't copy from '$runner' to '$toname': $!";
172 close $from
173 or die "Can't close '$runner': $!";
174 close $to
175 or die "Can't close '$toname': $!";
176 chmod 0500, $toname
177 or die "Can't chmod 0500, '$toname': $!";
178 $runner = $toname;
179 system $^X, $runner, '--check-args', @ARGV
180 and die "Can't run inplace for some reason. :-(";
181 }
182}
6a8dbfd7 183
195ed8b1
NC
184sub validate {
185 my $commit = shift;
3f14869b
NC
186 if (defined $start && `git rev-list -n1 $commit ^$start^` eq "") {
187 print "Skipping $commit, as it is earlier than $start\n";
188 return;
189 }
190 if (defined $end && `git rev-list -n1 $end ^$commit^` eq "") {
191 print "Skipping $commit, as it is more recent than $end\n";
192 return;
193 }
195ed8b1
NC
194 print "Testing $commit...\n";
195 system "git checkout $commit </dev/null" and die;
196 my $ret = system $^X, $runner, '--no-clean', @ARGV;
197 die "Runner returned $ret, not 0 for revision $commit" if $ret;
198 system 'git clean -dxf </dev/null' and die;
199 system 'git reset --hard HEAD </dev/null' and die;
e2760528 200 return $commit;
195ed8b1
NC
201}
202
203if ($validate) {
e2760528
NC
204 require Text::Wrap;
205 my @built = map {validate $_} 'blead', reverse @stable;
206 if (@built) {
207 print Text::Wrap::wrap("", "", "Successfully validated @built\n");
208 exit 0;
209 }
210 print "Did not validate anything\n";
211 exit 1;
195ed8b1
NC
212}
213
c382b335
NC
214my $git_version = `git --version`;
215if (defined $git_version
216 && $git_version =~ /\Agit version (\d+\.\d+\.\d+)(.*)/) {
217 $git_version = eval "v$1";
218} else {
219 $git_version = v0.0.0;
220}
221
222if ($git_version ge v1.6.6) {
223 system "git bisect reset HEAD" and die;
224} else {
225 system "git bisect reset" and die;
226}
6a8dbfd7 227
6a8dbfd7 228# Sanity check the first and last revisions:
c382b335
NC
229system "git checkout $end" and die;
230my $ret = system $^X, $runner, @ARGV;
231die "Runner returned $ret for end revision" unless $ret;
a84ce349
NC
232die "Runner returned $ret for end revision, which is a skip"
233 if $ret == 125 * 256;
c382b335 234
4819dd2a
NC
235if (defined $start) {
236 system "git checkout $start" and die;
e295b7be 237 my $ret = system $^X, $runner, @ARGV;
4819dd2a
NC
238 die "Runner returned $ret, not 0 for start revision" if $ret;
239} else {
240 # Try to find the earliest version for which the test works
03cc0342 241 my @tried;
4819dd2a 242 foreach my $try (@stable) {
03cc0342
NC
243 if (`git rev-list -n1 $end ^$try^` eq "") {
244 print "Skipping $try, as it is more recent than end commit "
245 . (substr $end, 0, 16) . "\n";
246 # As @stable is supposed to be in age order, arguably we should
247 # last; here.
248 next;
249 }
4819dd2a 250 system "git checkout $try" and die;
e295b7be 251 my $ret = system $^X, $runner, @ARGV;
4819dd2a
NC
252 if (!$ret) {
253 $start = $try;
254 last;
255 }
03cc0342 256 push @tried, $try;
4819dd2a 257 }
03cc0342 258 die "Can't find a suitable start revision to default to.\nTried @tried"
4819dd2a
NC
259 unless defined $start;
260}
6a8dbfd7
NC
261
262system "git bisect start" and die;
263system "git bisect good $start" and die;
264system "git bisect bad $end" and die;
265
266# And now get git bisect to do the hard work:
e295b7be 267system 'git', 'bisect', 'run', $^X, $runner, @ARGV and die;
6a8dbfd7 268
c3d98a71
NC
269END {
270 my $end_time = time;
6a8dbfd7 271
625770c2 272 printf "That took %d seconds.\n", $end_time - $start_time
c3d98a71
NC
273 if defined $start_time;
274}
275
9b404864
NC
276=for comment
277
278Documentation for this is in bisect-runner.pl
279
280=cut
281
c3d98a71 282# ex: set ts=8 sts=4 sw=4 et: