2 ################################################################################
4 # scanprov -- scan Perl headers for provided macros, and add known exceptions
6 # The lines added have a code to signify they are added by us:
7 # M means it is a macro
8 # X means it is a known exceptional item
10 ################################################################################
12 # Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
13 # Version 2.x, Copyright (C) 2001, Paul Marquess.
14 # Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
16 # This program is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
19 ################################################################################
24 require './parts/ppptools.pl';
25 require './parts/inc/inctools';
26 require './devel/devtools.pl';
30 install => '/tmp/perl/install/default',
36 GetOptions(\%opt, qw( install=s mode=s blead=s debug=i debug-start=s)) or die;
38 my $write = $opt{mode} eq 'write';
40 my %embed = map { ( $_->{name} => 1 ) }
41 parse_embed(qw(parts/embed.fnc parts/apidoc.fnc parts/ppport.fnc ));
43 # @provided is set to the elements that are provided, but not functions in the
45 my @provided = grep { !exists $embed{$_} }
46 map { /^(\w+)/ ? $1 : () }
47 `$^X ppport.h --list-provided`;
49 # There are a few exceptions that have to be dealt with specially. Add these
50 # to the list of things to scan for.
51 my $hard_to_test_ref = known_but_hard_to_test_for();
52 push @provided, keys %$hard_to_test_ref;
54 my $base_dir = 'parts/base';
55 my $todo_dir = 'parts/todo';
59 # Get the list of files, which are returned sorted, and so the min version
60 # is in the 0th element
61 my @files = all_files_in_dir($base_dir);
64 $min_perl =~ s,.*/,,; # The name is the integer of __MIN_PERL__
66 # There are a very few special cases that we may not find in scanning, but
67 # exist all the way back. Add them now to avoid throwing later things
69 print "-- $file --\n";
70 open F, ">>$file" or die "$file: $!\n";
71 for (qw(RETVAL CALL THIS)) { # These are also in hard_to_test_for(),
72 # so can't be in blead, as they are skipped
73 # in testing, so no real need to check that
75 print "Adding $_ to $file\n";
76 print F format_output_line($_, 'X');
80 # Now we're going to add the hard to test symbols. The hash has been
81 # manually populated and commited, with the version number ppport supports
84 # This is a hash ref with the keys being all symbols found in all the
85 # files in the directory, and the values being the perl versions of each
87 my $todo = parse_todo($todo_dir);
89 # The keys of $hard_to_test_ref are the symbols, and the values are
90 # subhashes, with each 'version' key being its proper perl version.
91 # Below, we invert %hard_to_test, so that the keys are the version, and
92 # the values are the symbols that go in that version
94 for my $hard (keys %$hard_to_test_ref) {
96 # But if someone ups the min version we support, we don't want to add
97 # something less than that.
98 my $version = int_parse_version($hard_to_test_ref->{$hard});
99 $version = $min_perl if $version < $min_perl;
100 $version = format_version_line($version);
102 push @{$add_by_version{$version}}, $hard
103 unless grep { $todo->{$_}->{version} eq $hard } keys %$todo;
106 # Only a few files will have exceptions that apply to them. Rewrite each
107 foreach my $version (keys %add_by_version) {
108 my $file = "$todo_dir/" . int_parse_version($version);
109 print "-- Adding known exceptions to $file --\n";
110 my $need_version_line = ! -e $file;
111 open F, ">>$file" or die "$file: $!\n";
112 print F format_version_line($version) . "\n" if $need_version_line;
113 foreach my $symbol (sort dictionary_order @{$add_by_version{$version}})
115 print "adding $symbol\n";
116 print F format_output_line($symbol, 'X');
122 # Now that we've added the exceptions to a few files, we can parse
123 # and deal with all of them.
124 my $perls_ref = get_and_sort_perls(\%opt);
126 die "Couldn't find any perls" unless @$perls_ref > 1;
130 # We look in descending order of perl versions. Each time through the loop
131 # @provided is narrowed.
132 for my $p (@$perls_ref) {
133 print "checking perl $p->{version}...\n";
135 # Get the hdr files associated with this version
136 my $archlib = `$p->{path} -MConfig -l -e 'print \$Config{archlib}'`;
138 local @ARGV = glob "$archlib/CORE/*.h";
141 # %sym's keys are every single thing that looks like an identifier
142 # (beginning with a non-digit \w, followed by \w*) that occurs in all the
143 # headers, regardless of where (outside of comments).
145 while (<>) { # Read in the next file
147 # Strip comments, from perl faq
148 s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse;
150 $sym{$_}++ for /(\b[^\W\d]\w*)/g;
153 # @provided is narrowed to include only those identifier-like things that
154 # are mentioned in some hdr in this release. (If it isn't even mentioned,
155 # it won't exist in the release.) For those not mentioned, a key is added
156 # of the identifier-like thing in %v. It is a subkey of this release's
157 # "todo" release, which is the next higher one. If we are at version n, we
158 # have already done version n+1 and the provided element was mentioned
159 # there, and now it no longer is. We take that to mean that to mean that
160 # the element became provided for in n+1.
161 @provided = map { $sym{$_} or $v{$p->{todo}}{$_}++;
166 # Read in the parts/base files. The hash ref has keys being all symbols found
167 # in all the files in base/, and the values being the perl versions each symbol
169 my $out = 'parts/base';
170 my $base_ref = parse_todo($out);
172 # Now add the results from above. At this point, The keys of %v are the 7
173 # digit BCD version numbers, and their subkeys are the symbols provided by
174 # D:P that are first mentioned in this version, like this:
176 # 'MY_CXT_CLONE' => 1,
178 # 'UTF8_MAXBYTES' => 1
181 for my $v (keys %v) {
183 # Things listed in blead (the most recent file) are special. They are there
184 # by default because we haven't found them anywhere, so they don't really
185 # exist as far as we can determine, so shouldn't be listed as existing.
186 next if $v > $perls_ref->[0]->{file};
188 # @new becomes the symbols for version $v not already in the file for $v
189 my @new = sort dictionary_order grep { !exists $base_ref->{$_} }
191 @new or next; # Nothing new, skip writing
195 $file = "$out/$file";
196 -e $file or die "non-existent: $file\n";
197 print "-- $file --\n";
198 $write and (open F, ">>$file" or die "$file: $!\n");
201 $write and print F format_output_line($_, 'M');
206 sub format_output_line
211 return sprintf "%-30s # $code added by $0\n", $sym;