This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlapi: Remove per-thread section; move to real scns
[perl5.git] / Porting / checkcfgvar.pl
1 #!/usr/bin/perl
2
3 # Check that the various config.sh-clones have (at least) all the
4 # same symbols as the top-level config_h.SH so that the (potentially)
5 # needed symbols are not lagging after how Configure thinks the world
6 # is laid out.
7 #
8 # VMS is probably not handled properly here, due to their own
9 # rather elaborate DCL scripting.
10 #
11
12 use strict;
13 use warnings;
14 use autodie;
15
16 sub usage
17 {
18     my $err = shift and select STDERR;
19     print "usage: $0 [--list] [--regen] [--default=value]\n";
20     exit $err;
21     } # usage
22
23 use Getopt::Long;
24 my $opt_l = 0;
25 my $opt_r = 0;
26 my $default;
27 my $tap = 0;
28 my $test;
29 GetOptions (
30     "help|?"    => sub { usage (0); },
31     "l|list!"   => \$opt_l,
32     "regen"     => \$opt_r,
33     "default=s" => \$default,
34     "tap"       => \$tap,
35     ) or usage (1);
36
37 $default and $default =~ s/^'(.*)'$/$1/; # Will be quoted on generation
38
39 require './regen/regen_lib.pl' if $opt_r;
40
41 my $MASTER_CFG = "config_h.SH";
42 # Inclusive bounds on the main part of the file, $section == 1 below:
43 my $first = qr/^Author=/;
44 my $last = qr/^zip=/;
45
46 my @CFG = (
47            # we check from MANIFEST whether they are expected to be present.
48            # We can't base our check on $], because that's the version of the
49            # perl that we are running, not the version of the source tree.
50            "Cross/config.sh-arm-linux",
51            "Cross/config.sh-arm-linux-n770",
52            "NetWare/config.wc",
53            "uconfig.sh",
54            "uconfig64.sh",
55            "plan9/config_sh.sample",
56            "win32/config.gc",
57            "win32/config.vc",
58            "configure.com",
59            "Porting/config.sh",
60           );
61
62 my @MASTER_CFG;
63 {
64     my %seen;
65     open my $fh, '<', $MASTER_CFG;
66     while (<$fh>) {
67         while (/[^\\]\$([a-z]\w+)/g) {
68             my $v = $1;
69             next if $v =~ /^(CONFIG_H|CONFIG_SH)$/;
70             $seen{$v}++;
71         }
72     }
73     close $fh;
74     @MASTER_CFG = sort keys %seen;
75 }
76
77 my %MANIFEST;
78
79 {
80     open my $fh, '<', 'MANIFEST';
81     while (<$fh>) {
82         $MANIFEST{$1}++ if /^(.+?)\t/;
83     }
84     close $fh;
85 }
86
87 printf "1..%d\n", 2 * @CFG if $tap;
88
89 for my $cfg (sort @CFG) {
90     unless (exists $MANIFEST{$cfg}) {
91         print STDERR "[skipping not-expected '$cfg']\n";
92         next;
93     }
94     my %cfg;
95     my $section = 0;
96     my @lines;
97
98     open my $fh, '<', $cfg;
99
100     if ($cfg eq 'configure.com') {
101         ++$cfg{startperl}; # Cheat.
102
103         while (<$fh>) {
104             next if /^\#/ || /^\s*$/ || /^\:/;
105             s/(\s*!.*|\s*)$//; # remove trailing comments or whitespace
106             ++$cfg{$1} if /^\$\s+WC "(\w+)='(?:.*)'"$/;
107         }
108     } else {
109         while (<$fh>) {
110             if ($_ =~ $first) {
111                 die "$cfg:$.:section=$section:$_" unless $section == 0;
112                 $section = 1;
113             }
114             push @{$lines[$section]}, $_;
115             next if /^\#/ || /^\s*$/ || /^\:/;
116             if ($_ =~ $last) {
117                 die "$cfg:$.:section=$section:$_" unless $section == 1;
118                 $section = 2;
119             }
120             # foo='bar'
121             # foo=bar
122             # (optionally with a trailing comment)
123             if (/^(\w+)=(?:'.*'|[^'].*)(?: #.*)?$/) {
124                 ++$cfg{$1};
125             } else {
126                 warn "$cfg:$.:$_";
127             }
128         }
129     }
130     close $fh;
131
132     ++$test;
133     my $missing;
134     if ($cfg eq 'configure.com') {
135         print "ok $test # skip $cfg doesn't need to be sorted\n"
136             if $tap;
137     } elsif (join("", @{$lines[1]}) eq join("", sort @{$lines[1]})) {
138         print "ok $test - $cfg sorted\n"
139             if $tap;
140     } elsif ($tap) {
141         print "not ok $test - $cfg is not sorted\n";
142     } elsif ($opt_r || $opt_l) {
143         # A reference to an empty array is true, hence this flags the
144         # file for later attention by --regen and --list, even if
145         # nothing is missing. Actual sort and output are done later.
146         $missing = [];
147     } else {
148         print "$cfg: unsorted\n"
149     }
150
151     for my $v (@MASTER_CFG) {
152         # This only creates a reference in $missing if something is missing:
153         push @$missing, $v unless exists $cfg{$v};
154     }
155
156     ++$test;
157     if ($missing) {
158         if ($tap) {
159             print "not ok $test - $cfg missing keys @$missing\n";
160         } elsif ($opt_l) {
161             # print the name once, however many problems
162             print "$cfg\n";
163         } elsif ($opt_r && $cfg ne 'configure.com') {
164             if (defined $default) {
165                 push @{$lines[1]}, map {"$_='$default'\n"} @$missing;
166             } else {
167                 print "$cfg: missing '$_', use --default to add it\n"
168                     foreach @$missing;
169             }
170
171             @{$lines[1]} = sort @{$lines[1]};
172             my $fh = open_new($cfg);
173             print $fh @{$_} foreach @lines;
174             close_and_rename($fh);
175         } else {
176             print "$cfg: missing '$_'\n" foreach @$missing;
177         }
178     } elsif ($tap) {
179         print "ok $test - $cfg has no missing keys\n";
180     }
181 }