This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
d3aeab2ff307e256e448378dee0bd553b7eea1cd
[perl5.git] / dist / Devel-PPPort / devel / mktodo.pl
1 #!/usr/bin/perl -w
2 ################################################################################
3 #
4 #  mktodo.pl -- generate baseline and todo files
5 #
6 ################################################################################
7 #
8 #  Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
9 #  Version 2.x, Copyright (C) 2001, Paul Marquess.
10 #  Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
11 #
12 #  This program is free software; you can redistribute it and/or
13 #  modify it under the same terms as Perl itself.
14 #
15 ################################################################################
16
17 use strict;
18 use Getopt::Long;
19 use Data::Dumper;
20 use IO::File;
21 use IO::Select;
22 use Config;
23 use Time::HiRes qw( gettimeofday tv_interval );
24
25 require './devel/devtools.pl';
26
27 our %opt = (
28   blead     => 0,     # ? Is this perl blead
29   debug   => 0,
30   base    => 0,
31   verbose => 0,
32   check   => 1,
33   todo    => "",  # If no --todo, this is a blead perl
34   shlib   => 'blib/arch/auto/Devel/PPPort/PPPort.so',
35 );
36
37 GetOptions(\%opt, qw(
38 perl=s todo=s blead version=s shlib=s debug base verbose check!
39           )) or die;
40
41 identify();
42
43 print "\n", ident_str(), "\n\n";
44
45 my $fullperl = `which $opt{perl}`;
46 chomp $fullperl;
47
48 $ENV{SKIP_SLOW_TESTS} = 1;
49
50 regen_Makefile();
51
52 my %stdsym = map { ($_ => 1) } qw (
53   strlen
54   snprintf
55   strcmp
56   memcpy
57   strncmp
58   memmove
59   memcmp
60   tolower
61   exit
62   memset
63   vsnprintf
64   siglongjmp
65   sprintf
66 );
67
68 my %sym;
69 for (`$Config{nm} $fullperl`) {
70   chomp;
71   /\s+T\s+(\w+)\s*$/ and $sym{$1}++;
72 }
73 keys %sym >= 50 or die "less than 50 symbols found in $fullperl\n";
74
75 my %todo = %{load_todo($opt{todo}, $opt{version})} if $opt{todo};
76 my @recheck;
77
78 my $symmap = get_apicheck_symbol_map();
79
80 for (;;) {
81   my $retry = 1;
82   my $trynm = 1;
83   regen_apicheck();
84
85 retry:
86   my(@new, @already_in_sym, %seen);
87
88   my $r = run(qw(make));
89   $r->{didnotrun} and die "couldn't run make: $!\n" .
90         join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
91
92   # If there were warnings, we ask the user before continuing when creating
93   # the base files of blead.  This leads to a potential early exit when things
94   # aren't working right.
95   if ($opt{blead} && $opt{base}) {
96     undef $opt{blead};  # Only warn once.
97     if (@{$r->{stderr}}) {
98         print STDERR "Warnings and errors from compiling blead:\n";
99         print STDERR @{$r->{stderr}};
100         ask_or_quit("\nUnexpected warnings when compiling blead can lead to"
101                   . " wrong results.  Please examine the above list.\n"
102                   . "Shall I proceed?");
103     }
104     else {
105         print STDERR "blead compiled without warnings nor errors.\n"
106                    . "Proceeding with everything else\n";
107     }
108   }
109
110   for my $l (@{$r->{stderr}}) {
111     if ($l =~ /_DPPP_test_(\w+)/) {
112       if (!$seen{$1}++) {
113         my @s = grep { exists $sym{$_} } $1, "Perl_$1", "perl_$1";
114         if (@s) {
115           push @already_in_sym, [$1, "E (@s)"];
116         }
117         else {
118           push @new, [$1, "E"];
119         }
120       }
121     }
122   }
123
124   if ($r->{status} == 0) {
125     my @u;
126     my @usym;
127
128     if ($trynm) {
129       @u = eval { find_undefined_symbols($fullperl, $opt{shlib}) };
130       warn "warning: $@" if $@;
131       $trynm = 0;
132     }
133
134     unless (@u) {
135       $r = run(qw(make test));
136       $r->{didnotrun} and die "couldn't run make test: $!\n" .
137         join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
138       $r->{status} == 0 and last;
139
140       for my $l (@{$r->{stderr}}) {
141         if ($l =~ /undefined symbol: (\w+)/) {
142           push @u, $1;
143         }
144       }
145     }
146
147     for my $u (@u) {
148       for my $m (keys %{$symmap->{$u}}) {
149         if (!$seen{$m}++) {
150           my $pl = $m;
151           $pl =~ s/^[Pp]erl_//;
152           my @s = grep { exists $sym{$_} } $pl, "Perl_$pl", "perl_$pl";
153           push @new, [$m, @s ? "U (@s)" : "U"];
154         }
155       }
156     }
157   }
158
159   @new = grep !$todo{$_->[0]}, @new;
160
161   unless (@new) {
162     @new = grep !$todo{$_->[0]}, @already_in_sym;
163   }
164
165   unless (@new) {
166     if ($retry > 0) {
167       $retry--;
168       regen_Makefile();
169       goto retry;
170     }
171     print Dumper($r);
172     die "no new TODO symbols found...";
173   }
174
175   # don't recheck undefined symbols reported by the dynamic linker
176   push @recheck, map { $_->[0] } grep { $_->[1] !~ /^U/ } @new;
177
178   for (@new) {
179     display_sym('new', @$_);
180     $todo{$_->[0]} = $_->[1];
181   }
182
183   write_todo($opt{todo}, $opt{version}, \%todo);
184 }
185
186 if ($opt{check}) {
187   my $ifmt = '%' . length(scalar @recheck) . 'd';
188   my $t0 = [gettimeofday];
189
190   RECHECK: for my $i (0 .. $#recheck) {
191     my $sym = $recheck[$i];
192     my $cur = delete $todo{$sym};
193
194     display_sym('chk', $sym, $cur, sprintf(" [$ifmt/$ifmt, ETA %s]",
195                $i + 1, scalar @recheck, eta($t0, $i, scalar @recheck)));
196
197     write_todo($opt{todo}, $opt{version}, \%todo);
198
199     if ($cur eq "E (Perl_$sym)") {
200       # we can try a shortcut here
201       regen_apicheck($sym);
202
203       my $r = run(qw(make test));
204
205       if (!$r->{didnotrun} && $r->{status} == 0) {
206         display_sym('del', $sym, $cur);
207         next RECHECK;
208       }
209     }
210
211     # run the full test
212     regen_Makefile();
213
214     my $r = run(qw(make test));
215
216     $r->{didnotrun} and die "couldn't run make test: $!\n" .
217         join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
218
219     if ($r->{status} == 0) {
220       display_sym('del', $sym, $cur);
221     }
222     else {
223       $todo{$sym} = $cur;
224     }
225   }
226 }
227
228 write_todo($opt{todo}, $opt{version}, \%todo);
229
230 run(qw(make realclean));
231
232 exit 0;
233
234 sub display_sym
235 {
236   my($what, $sym, $reason, $extra) = @_;
237   $extra ||= '';
238   my %col = (
239     'new' => 'bold red',
240     'chk' => 'bold magenta',
241     'del' => 'bold green',
242   );
243   $what = colored("$what symbol", $col{$what});
244
245   printf "[%s] %s %-30s # %s%s\n",
246          $opt{version}, $what, $sym, $reason, $extra;
247 }
248
249 sub regen_Makefile
250 {
251   my @mf_arg = ('--with-apicheck', 'OPTIMIZE=-O0 -w');
252   push @mf_arg, qw( DEFINE=-DDPPP_APICHECK_NO_PPPORT_H ) if $opt{base};
253
254   # just to be sure
255   run(qw(make realclean));
256   my $r = run($fullperl, "Makefile.PL", @mf_arg);
257   unless ($r->{status} == 0) {
258       die "cannot run Makefile.PL: $!\n" .
259           join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
260   }
261 }
262
263 sub regen_apicheck
264 {
265   unlink qw(apicheck.c apicheck.o);
266   runtool({ out => '/dev/null' }, $fullperl, 'apicheck_c.PL', map { "--api=$_" } @_)
267       or die "cannot regenerate apicheck.c\n";
268 }
269
270 sub load_todo
271 {
272   my($file, $expver) = @_;
273
274   if (-e $file) {
275     my $f = new IO::File $file or die "cannot open $file: $!\n";
276     my $ver = <$f>;
277     chomp $ver;
278     if ($ver eq $expver) {
279       my %sym;
280       while (<$f>) {
281         chomp;
282         /^(\w+)\s+#\s+(.*)/ or goto nuke_file;
283         exists $sym{$1} and goto nuke_file;
284         $sym{$1} = $2;
285       }
286       return \%sym;
287     }
288
289 nuke_file:
290     undef $f;
291     unlink $file or die "cannot remove $file: $!\n";
292   }
293
294   return {};
295 }
296
297 sub write_todo
298 {
299   my($file, $ver, $sym) = @_;
300   my $f;
301
302   $f = new IO::File ">$file" or die "cannot open $file: $!\n";
303   $f->print("$ver\n");
304
305   # Dictionary ordering, with only alphanumerics
306   for (sort dictionary_order keys %$sym) {
307     $f->print(sprintf "%-30s # %s\n", $_, $sym->{$_});
308   }
309 }
310
311 sub find_undefined_symbols
312 {
313   my($perl, $shlib) = @_;
314
315   my $ps = read_sym(file => $perl,  options => [qw( --defined-only   )]);
316   my $ls = read_sym(file => $shlib, options => [qw( --undefined-only )]);
317
318   my @undefined;
319
320   for my $sym (keys %$ls) {
321     unless (exists $ps->{$sym}) {
322       if ($sym !~ /\@/ and $sym !~ /^_/) {
323         push @undefined, $sym unless $stdsym{$sym};
324       }
325     }
326   }
327
328   return @undefined;
329 }
330
331 sub read_sym
332 {
333   my %opt = ( options => [], @_ );
334
335   my $r = run($Config{nm}, @{$opt{options}}, $opt{file});
336
337   if ($r->{didnotrun} or $r->{status}) {
338     die "cannot run $Config{nm}" .
339           join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
340   }
341
342   my %sym;
343
344   for (@{$r->{stdout}}) {
345     chomp;
346     my($adr, $fmt, $sym) = /^\s*([[:xdigit:]]+)?\s+([ABCDGINRSTUVW?-])\s+(\S+)\s*$/i
347                            or die "cannot parse $Config{nm} output:\n[$_]\n";
348     $sym{$sym} = { format => $fmt };
349     $sym{$sym}{address} = $adr if defined $adr;
350   }
351
352   return \%sym;
353 }
354
355 sub get_apicheck_symbol_map
356 {
357   my $r;
358
359   while (1) {
360
361     # Create apicheck.i
362     $r = run(qw(make apicheck.i));
363
364     # Quit the loop if it succeeded
365     last unless $r->{didnotrun} or $r->{status};
366
367     # Get the list of macros that it failed on
368     my %sym = map { /error: macro "(\w+)" (?:requires|passed) \d+ argument/ ? ($1 => 'A') : () }
369               @{$r->{stderr}};
370
371     if (keys %sym) {
372       for my $s (sort dictionary_order keys %sym) {
373         display_sym('new', $s, $sym{$s});
374         $todo{$s} = $sym{$s};
375       }
376       write_todo($opt{todo}, $opt{version}, \%todo);
377       regen_apicheck();
378     }
379     else {  # It failed for some other reason: give up
380       die "cannot run make apicheck.i ($r->{didnotrun} / $r->{status}):\n".
381           join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
382     }
383   }
384
385   my $fh = IO::File->new('apicheck.i')
386            or die "cannot open apicheck.i: $!";
387
388   local $_;
389   my %symmap;
390   my $cur;
391
392   while (<$fh>) {
393     next if /^#/;
394     if (defined $cur) {
395       for my $sym (/\b([A-Za-z_]\w+)\b/g) {
396         $symmap{$sym}{$cur}++;
397       }
398       undef $cur if /^}$/;
399     }
400     else {
401       /_DPPP_test_(\w+)/ and $cur = $1;
402     }
403   }
404
405   return \%symmap;
406 }