This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump version to 5.33.5
[perl5.git] / Porting / checkcfgvar.pl
CommitLineData
acad74ad 1#!/usr/bin/perl
522c08cc 2
522c08cc
RGS
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#
a0e39d89
AT
8# VMS is probably not handled properly here, due to their own
9# rather elaborate DCL scripting.
522c08cc
RGS
10#
11
12use strict;
acad74ad 13use warnings;
4ccedf19 14use autodie;
acad74ad
MB
15
16sub usage
17{
18 my $err = shift and select STDERR;
818a19bd 19 print "usage: $0 [--list] [--regen] [--default=value]\n";
acad74ad
MB
20 exit $err;
21 } # usage
22
23use Getopt::Long;
24my $opt_l = 0;
7ee20c71 25my $opt_r = 0;
818a19bd 26my $default;
6692294e
NC
27my $tap = 0;
28my $test;
acad74ad
MB
29GetOptions (
30 "help|?" => sub { usage (0); },
31 "l|list!" => \$opt_l,
7ee20c71 32 "regen" => \$opt_r,
818a19bd 33 "default=s" => \$default,
6692294e 34 "tap" => \$tap,
acad74ad 35 ) or usage (1);
522c08cc 36
0d70e70b
MB
37$default and $default =~ s/^'(.*)'$/$1/; # Will be quoted on generation
38
3d7c117d 39require './regen/regen_lib.pl' if $opt_r;
7ee20c71 40
522c08cc 41my $MASTER_CFG = "config_h.SH";
7ee20c71
NC
42# Inclusive bounds on the main part of the file, $section == 1 below:
43my $first = qr/^Author=/;
44my $last = qr/^zip=/;
522c08cc
RGS
45
46my @CFG = (
522c08cc 47 # we check from MANIFEST whether they are expected to be present.
bb0fc361
NC
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.
522c08cc 50 "Cross/config.sh-arm-linux",
bd1f84ac 51 "Cross/config.sh-arm-linux-n770",
522c08cc 52 "NetWare/config.wc",
522c08cc 53 "uconfig.sh",
fc09a9a0 54 "uconfig64.sh",
522c08cc 55 "plan9/config_sh.sample",
522c08cc
RGS
56 "win32/config.gc",
57 "win32/config.vc",
a0e39d89 58 "configure.com",
d73cfe96 59 "Porting/config.sh",
522c08cc
RGS
60 );
61
2dcb77c2 62my @MASTER_CFG;
4ccedf19 63{
2dcb77c2 64 my %seen;
4ccedf19
NC
65 open my $fh, '<', $MASTER_CFG;
66 while (<$fh>) {
2eacba2f 67 while (/[^\\]\$([a-z]\w+)/g) {
522c08cc
RGS
68 my $v = $1;
69 next if $v =~ /^(CONFIG_H|CONFIG_SH)$/;
2dcb77c2 70 $seen{$v}++;
522c08cc
RGS
71 }
72 }
4ccedf19 73 close $fh;
2dcb77c2 74 @MASTER_CFG = sort keys %seen;
522c08cc
RGS
75}
76
522c08cc
RGS
77my %MANIFEST;
78
4ccedf19
NC
79{
80 open my $fh, '<', 'MANIFEST';
81 while (<$fh>) {
82 $MANIFEST{$1}++ if /^(.+?)\t/;
83 }
84 close $fh;
85}
522c08cc 86
6692294e
NC
87printf "1..%d\n", 2 * @CFG if $tap;
88
68cf6151 89for my $cfg (sort @CFG) {
522c08cc 90 unless (exists $MANIFEST{$cfg}) {
a76803d0 91 print STDERR "[skipping not-expected '$cfg']\n";
522c08cc
RGS
92 next;
93 }
94 my %cfg;
7ee20c71
NC
95 my $section = 0;
96 my @lines;
4ccedf19
NC
97
98 open my $fh, '<', $cfg;
f4ca0be2
NC
99
100 if ($cfg eq 'configure.com') {
101 ++$cfg{startperl}; # Cheat.
102
103 while (<$fh>) {
104 next if /^\#/ || /^\s*$/ || /^\:/;
4ccedf19 105 s/(\s*!.*|\s*)$//; # remove trailing comments or whitespace
f4ca0be2 106 ++$cfg{$1} if /^\$\s+WC "(\w+)='(?:.*)'"$/;
4ccedf19 107 }
f4ca0be2
NC
108 } else {
109 while (<$fh>) {
7ee20c71
NC
110 if ($_ =~ $first) {
111 die "$cfg:$.:section=$section:$_" unless $section == 0;
112 $section = 1;
113 }
114 push @{$lines[$section]}, $_;
f4ca0be2 115 next if /^\#/ || /^\s*$/ || /^\:/;
7ee20c71
NC
116 if ($_ =~ $last) {
117 die "$cfg:$.:section=$section:$_" unless $section == 1;
118 $section = 2;
119 }
f4ca0be2
NC
120 # foo='bar'
121 # foo=bar
58f1e530
NC
122 # (optionally with a trailing comment)
123 if (/^(\w+)=(?:'.*'|[^'].*)(?: #.*)?$/) {
f4ca0be2
NC
124 ++$cfg{$1};
125 } else {
126 warn "$cfg:$.:$_";
127 }
4ccedf19
NC
128 }
129 }
130 close $fh;
131
6692294e 132 ++$test;
e1bac119 133 my $missing;
6692294e
NC
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";
e1bac119
NC
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"
7ee20c71 149 }
e1bac119 150
1750d3ff 151 for my $v (@MASTER_CFG) {
e1bac119
NC
152 # This only creates a reference in $missing if something is missing:
153 push @$missing, $v unless exists $cfg{$v};
1750d3ff 154 }
e1bac119 155
6692294e 156 ++$test;
e1bac119 157 if ($missing) {
6692294e
NC
158 if ($tap) {
159 print "not ok $test - $cfg missing keys @$missing\n";
160 } elsif ($opt_l) {
e1bac119
NC
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 }
6692294e
NC
178 } elsif ($tap) {
179 print "ok $test - $cfg has no missing keys\n";
7ee20c71 180 }
522c08cc 181}