2 ################################################################################
4 # mktodo.pl -- generate baseline and todo files
6 # It makes the todo file for the single passed in perl binary. If --base is
7 # not specified it compiles with ppport.h.
8 ################################################################################
10 # Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
11 # Version 2.x, Copyright (C) 2001, Paul Marquess.
12 # Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
14 # This program is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
17 ################################################################################
25 use Time::HiRes qw( gettimeofday tv_interval );
27 require './devel/devtools.pl';
30 blead => 0, # ? Is this perl blead
31 debug => 0, # Adding --verbose increases the detail
32 base => 0, # Don't use ppport.h when generating
35 todo => "", # If no --todo, this is a blead perl
36 shlib => 'blib/arch/auto/Devel/PPPort/PPPort.so',
40 perl=s todo=s blead version=s shlib=s debug base verbose check!
48 $todo_file = $opt{todo};
49 $todo_version = $opt{version};
52 print "\n", ident_str(), "\n\n";
54 my $fullperl = `which $opt{perl}`;
57 $ENV{SKIP_SLOW_TESTS} = 1;
59 # Generate the Makefile using the passed in perl
62 # List of functions that are never considered undefined. Add to as necessary
63 my %stdsym = map { ($_ => 1) } qw (
79 # Initialize %sym so that the keys are all the Text symbols for this perl,
80 # output from the system's 'nm'
82 for (`$Config{nm} $fullperl`) {
84 /\s+T\s+(\w+)\s*$/ and $sym{$1}++;
86 keys %sym >= 50 or die "less than 50 symbols found in $fullperl\n";
88 # %todo is initialized to be the symbols in the current todo file, like so:
90 # 'UTF8_SAFE_SKIP' => 'U',
91 # 'newSVsv_flags' => 'U',
92 # 'newSVsv_nomg' => 'U',
95 # The values are the outputs from nm, plus 'E' from us, for Error
96 my %todo = %{load_todo($todo_file, $todo_version)} if $opt{todo};
100 # Get an exhaustive list from apicheck.i of symbols, what functions contain
101 # them, and how many in each function.
106 # 'toFOLD_uvchr' => 2,
107 # 'sv_uni_display' => 1,
110 my $symmap = get_apicheck_symbol_map();
112 # In each iteration of the loop we create an apicheck.c. This will contain a
113 # generated wrapper function for each API function and macro. The wrapper
114 # contains one or more calls to its API element. Then we attempt to compile
115 # apicheck.c into apicheck.o. If it compiles, then every API element exists
116 # in this version of perl. If not, we figure out which ones were undefined,
117 # and set things up so that in the next iteration of the loop, the wrappers
118 # for those elements are #ifdef'd out.
126 my(@new, @already_in_sym, %seen);
128 my $r = run(qw(make));
129 $r->{didnotrun} and die "couldn't run make: $!\n" .
130 join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
132 # If there were warnings, we ask the user before continuing when creating
133 # the base files of blead. This leads to a potential early exit when things
134 # aren't working right.
135 if ($opt{blead} && $opt{base}) {
136 undef $opt{blead}; # Only warn once.
137 if (@{$r->{stderr}}) {
138 print STDERR "Warnings and errors from compiling blead:\n";
139 print STDERR @{$r->{stderr}};
140 ask_or_quit("\nUnexpected warnings when compiling blead can lead to"
141 . " wrong results. Please examine the above list.\n"
142 . "Shall I proceed?");
145 print STDERR "blead compiled without warnings nor errors.\n"
146 . "Proceeding with everything else\n";
150 # Examine stderr. For each wrapper function listed in it, we create an
151 # 'E' (for error) entry. If the function (possibly prefixed by '[Pp]erl')
152 # is in %sym, it is added to @already_in_sym. Otherwise, @new.
153 for my $l (@{$r->{stderr}}) {
154 if ($l =~ /_DPPP_test_(\w+)/) {
156 my @s = grep { exists $sym{$_} } $1, "Perl_$1", "perl_$1";
158 push @already_in_sym, [$1, "E (@s)"];
161 push @new, [$1, "E"];
167 if ($r->{status} == 0) {
171 # Here, apicheck.o was successfully created. It likely will need
172 # functions from outside it in order to form a complete executable a.out.
173 # In the first iteration, look to see if all needed externs are available.
174 # (We don't actually try to create an a.out)
176 @u = eval { find_undefined_symbols($fullperl, $opt{shlib}) };
177 warn "warning: $@" if $@;
181 # If it didn't find any undefined symbols, everything should be working.
182 # Run the test suite.
184 $r = run(qw(make test));
185 $r->{didnotrun} and die "couldn't run make test: $!\n" .
186 join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
188 $r->{status} == 0 and last; # It worked!!
190 # Alas, something was wrong. Add any undefined symbols listed in the
192 for my $l (@{$r->{stderr}}) {
193 if ($l =~ /undefined symbol: (\w+)/) {
199 # For each undefined symbol
202 # If this is an API symbol, $symmap->{$u} will exist and be a hash of
203 # keys, being all the symbols referred to within it (with their values
204 # btw being the count of occurrences in the element).
205 for my $m (keys %{$symmap->{$u}}) {
208 $pl =~ s/^[Pp]erl_//;
209 my @s = grep { exists $sym{$_} } $pl, "Perl_$pl", "perl_$pl";
211 # The comment for this entry that goes into the file that gets
212 # written includes any [Pp]erl prefix.
213 push @new, [$m, @s ? "U (@s)" : "U"];
219 # Remove from @new all the current todo symbols
220 @new = grep !$todo{$_->[0]}, @new;
222 # If none remain, start over with those we know about, minus the todo
223 # symbols. khw doesn't understand why this is necessary
225 @new = grep !$todo{$_->[0]}, @already_in_sym;
228 # This retries once if nothing new was found (khw guesses that is just to
229 # be sure, or maybe it's because we ran nm the first time through)
237 die "no new TODO symbols found...";
240 # recheck symbols except undefined ones reported by the dynamic linker
241 push @recheck, map { $_->[0] } grep { $_->[1] !~ /^U/ } @new;
243 # Display each newly found undefined symbol, and add it to the list of todo
246 display_sym('new', @$_);
247 $todo{$_->[0]} = $_->[1];
250 # Write the revised todo, so that apicheck.c when generated in the next
251 # iteration will have these #ifdef'd out
252 write_todo($todo_file, $todo_version, \%todo);
255 # If we are to check our work, do so. This verifies that each symbol
256 # identified above is really a problem in this version. (khw doesn't know
257 # under what circumstances this becomes an issue)
259 # We go through each symbol on the @recheck list, and create an apicheck.c
263 # Create something like '%3d'
264 my $ifmt = '%' . length(scalar @recheck) . 'd';
266 my $t0 = [gettimeofday];
268 RECHECK: for my $i (0 .. $#recheck) {
269 my $sym = $recheck[$i];
271 # Assume it will work
272 my $cur = delete $todo{$sym};
274 # Give a progress report
275 display_sym('chk', $sym, $cur, sprintf(" [$ifmt/$ifmt, ETA %s]",
276 $i + 1, scalar @recheck, eta($t0, $i, scalar @recheck)));
278 # Write out the todo file without this symbol, meaning it will be enabled
279 # in the generated apicheck.c file
280 write_todo($todo_file, $todo_version, \%todo);
282 # E is not an nm symbol, but was added by us to indicate 'Error'
283 if ($cur eq "E (Perl_$sym)") {
285 # We can try a shortcut here. Create an apicheck.c file for just this
287 regen_apicheck($sym);
289 my $r = run(qw(make test));
291 if (!$r->{didnotrun} && $r->{status} == 0) {
293 # Shortcut indicated that this function compiles..
294 display_sym('del', $sym, $cur);
298 # Here, the api file with just this entry failed to compile. (khw
299 # doesn't know why we just don't give up on it now, but we don't.) We
300 # drop down below to generate and compile a full apicheck.c with this
301 # symbol enabled. (XXX Perhaps we could look at stderr and if it
302 # contained things about parameter mismatch, (which is a common
303 # occurrence), we could skip the steps below.)
306 # Either can't shortcut, or the shortcut indicated that the function
307 # doesn't compile in isolation. Create, compile and test with this
308 # function/symbol enabled. (Remember that this should have succeeded
309 # above to get to here when this symbol was disabled, so enabling just
310 # this one will tell us for sure that it works or doesn't work. (khw
311 # wonders if this is actually a DAG, or perhaps with cycles, so this is
312 # under it all, insufficient.)
315 my $r = run(qw(make test));
317 # This regenerated apicheck.c
318 dump_apicheck() if $opt{debug};
320 $r->{didnotrun} and die "couldn't run make test: $!\n" .
321 join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
323 if ($r->{status} == 0) { # This symbol compiles and tests ok, so retain
325 display_sym('del', $sym, $cur);
327 else { # Revert to this symbol is bad in this version
331 } # End of checking our work
333 write_todo($todo_file, $todo_version, \%todo);
335 # Clean up after ourselves
336 run(qw(make realclean));
342 my($what, $sym, $reason, $extra) = @_;
346 'chk' => 'bold magenta',
347 'del' => 'bold green',
349 $what = colored("$what symbol", $col{$what});
351 printf "[%s] %s %-30s # %s%s\n",
352 $todo_version, $what, $sym, $reason, $extra;
357 # We make sure to add rules for creating apicheck.c
358 my @mf_arg = ('--with-apicheck', 'OPTIMIZE=-O0 -w');
360 # It doesn't include ppport.h if generating the base files.
361 push @mf_arg, qw( DEFINE=-DDPPP_APICHECK_NO_PPPORT_H ) if $opt{base};
364 run(qw(make realclean));
366 my $r = run($fullperl, "Makefile.PL", @mf_arg);
367 unless ($r->{status} == 0) {
368 die "cannot run Makefile.PL: $!\n" .
369 join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
373 sub regen_apicheck # Regeneration can also occur by calling 'make'
375 unlink qw(apicheck.c apicheck.o);
376 runtool({ out => '/dev/null' }, $fullperl, 'apicheck_c.PL', map { "--api=$_" } @_)
377 or die "cannot regenerate apicheck.c\n";
378 dump_apicheck() if $opt{debug};
383 my $apicheck = "apicheck.c";
384 my $f = new IO::File $apicheck or die "cannot open $apicheck: $!\n";
386 print STDERR __FILE__, ": ", __LINE__, ": $apicheck (",
388 " lines) for $fullperl";
389 print STDERR " and '" if @_;
390 print STDERR join "', '", @_;
391 print STDERR "'" if @_;
394 print STDERR $n++, " ", $_ for @lines;
397 sub load_todo # Return entries from $file; skip if the first line
398 # isn't $expver (expected version)
400 my($file, $expver) = @_;
403 my $f = new IO::File $file or die "cannot open $file: $!\n";
406 if ($ver eq $expver) {
410 /^(\w+)\s+#\s+(.*)/ or goto nuke_file;
411 exists $sym{$1} and goto nuke_file;
419 unlink $file or die "cannot remove $file: $!\n";
425 sub write_todo # Write out the todo file. The keys of %sym are known to not
426 # be in this version, hence are 'todo'
428 my($file, $ver, $sym) = @_;
431 $f = new IO::File ">$file" or die "cannot open $file: $!\n";
434 # Dictionary ordering, with only alphanumerics
435 for (sort dictionary_order keys %$sym) {
436 $f->print(sprintf "%-30s # %s\n", $_, $sym->{$_});
442 sub find_undefined_symbols
444 # returns a list of undefined symbols in $shlib. To be considered
445 # undefined, it must also not be defined in $perl. Symbols that begin with
446 # underscore, or contain '@', or are some libc ones are not returned.
447 # Presumably, the list of libc could be expanded if necessary.
449 my($perl, $shlib) = @_;
451 my $ps = read_sym(file => $perl, options => [qw( --defined-only )]);
452 my $ls = read_sym(file => $shlib, options => [qw( --undefined-only )]);
456 for my $sym (keys %$ls) {
457 next if $sym =~ /\@/ or $sym =~ /^_/ or exists $stdsym{$sym};
458 unless (exists $ps->{$sym}) {
459 push @undefined, $sym;
468 my %opt = ( options => [], @_ );
470 my $r = run($Config{nm}, @{$opt{options}}, $opt{file});
472 if ($r->{didnotrun} or $r->{status}) {
473 die "cannot run $Config{nm}" .
474 join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
479 for (@{$r->{stdout}}) {
481 my($adr, $fmt, $sym) = /^\s*([[:xdigit:]]+)?\s+([ABCDGINRSTUVW?-])\s+(\S+)\s*$/i
482 or die "cannot parse $Config{nm} output:\n[$_]\n";
483 $sym{$sym} = { format => $fmt };
484 $sym{$sym}{address} = $adr if defined $adr;
490 sub get_apicheck_symbol_map
497 $r = run(qw(make apicheck.i));
499 # Quit the loop if it succeeded
500 last unless $r->{didnotrun} or $r->{status};
502 # Get the list of macros that had parameter issues. These are marked as
503 # A, for absolute in nm terms
504 my %sym = map { /error: macro "(\w+)" (?:requires|passed) \d+ argument/ ? ($1 => 'A') : () }
507 # Display these, and add them to the global %todo.
509 for my $s (sort dictionary_order keys %sym) {
510 display_sym('new', $s, $sym{$s});
511 $todo{$s} = $sym{$s};
514 # And rewrite the todo file, including these new symbols.
515 write_todo($todo_file, $todo_version, \%todo);
517 # Regenerate apicheck.c for the next iteration
520 else { # It failed for some other reason than parameter issues: give up
521 die "cannot run make apicheck.i ($r->{didnotrun} / $r->{status}):\n".
522 join('', @{$r->{stdout}})."\n---\n".join('', @{$r->{stderr}});
526 # Here, have an apicheck.i. Read it in
527 my $fh = IO::File->new('apicheck.i')
528 or die "cannot open apicheck.i: $!";
537 # We only care about lines within one of our _DPPP_test_ functions. If
538 # we're in one, $cur is set to the name of the current one.
539 if (! defined $cur) { # Not within such a function; see if this starts
541 /_DPPP_test_(\w+)/ and $cur = $1;
545 # For anything that looks like a symbol, note it as a key, and as its
546 # value, the name of the function. Actually the value is another key,
547 # whose value is the count of this symbol's occurrences, so it looks
551 # 'toFOLD_uvchr' => 2,
552 # 'sv_uni_display' => 1,
555 for my $sym (/\b([A-Za-z_]\w+)\b/g) {
556 $symmap{$sym}{$cur}++;
559 # This line marks the end of this function, as constructed by us.