This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Commit b776cec188 missed these RMG steps when preparing Module::CoreList for 5.19.11
[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
7ee20c71
NC
37require 'regen/regen_lib.pl' if $opt_r;
38
522c08cc 39my $MASTER_CFG = "config_h.SH";
7ee20c71
NC
40# Inclusive bounds on the main part of the file, $section == 1 below:
41my $first = qr/^Author=/;
42my $last = qr/^zip=/;
522c08cc
RGS
43
44my @CFG = (
522c08cc 45 # we check from MANIFEST whether they are expected to be present.
bb0fc361
NC
46 # We can't base our check on $], because that's the version of the
47 # perl that we are running, not the version of the source tree.
522c08cc 48 "Cross/config.sh-arm-linux",
522c08cc
RGS
49 "NetWare/config.wc",
50 "symbian/config.sh",
51 "uconfig.sh",
fc09a9a0 52 "uconfig64.sh",
522c08cc 53 "plan9/config_sh.sample",
522c08cc
RGS
54 "win32/config.gc",
55 "win32/config.vc",
26d21fa1 56 "win32/config.ce",
a0e39d89 57 "configure.com",
d73cfe96 58 "Porting/config.sh",
522c08cc
RGS
59 );
60
2dcb77c2 61my @MASTER_CFG;
4ccedf19 62{
2dcb77c2 63 my %seen;
4ccedf19
NC
64 open my $fh, '<', $MASTER_CFG;
65 while (<$fh>) {
2eacba2f 66 while (/[^\\]\$([a-z]\w+)/g) {
522c08cc
RGS
67 my $v = $1;
68 next if $v =~ /^(CONFIG_H|CONFIG_SH)$/;
2dcb77c2 69 $seen{$v}++;
522c08cc
RGS
70 }
71 }
4ccedf19 72 close $fh;
2dcb77c2 73 @MASTER_CFG = sort keys %seen;
522c08cc
RGS
74}
75
522c08cc
RGS
76my %MANIFEST;
77
4ccedf19
NC
78{
79 open my $fh, '<', 'MANIFEST';
80 while (<$fh>) {
81 $MANIFEST{$1}++ if /^(.+?)\t/;
82 }
83 close $fh;
84}
522c08cc 85
6692294e
NC
86printf "1..%d\n", 2 * @CFG if $tap;
87
68cf6151 88for my $cfg (sort @CFG) {
522c08cc 89 unless (exists $MANIFEST{$cfg}) {
a76803d0 90 print STDERR "[skipping not-expected '$cfg']\n";
522c08cc
RGS
91 next;
92 }
93 my %cfg;
7ee20c71
NC
94 my $section = 0;
95 my @lines;
4ccedf19
NC
96
97 open my $fh, '<', $cfg;
f4ca0be2
NC
98
99 if ($cfg eq 'configure.com') {
100 ++$cfg{startperl}; # Cheat.
101
102 while (<$fh>) {
103 next if /^\#/ || /^\s*$/ || /^\:/;
4ccedf19 104 s/(\s*!.*|\s*)$//; # remove trailing comments or whitespace
f4ca0be2 105 ++$cfg{$1} if /^\$\s+WC "(\w+)='(?:.*)'"$/;
4ccedf19 106 }
f4ca0be2
NC
107 } else {
108 while (<$fh>) {
7ee20c71
NC
109 if ($_ =~ $first) {
110 die "$cfg:$.:section=$section:$_" unless $section == 0;
111 $section = 1;
112 }
113 push @{$lines[$section]}, $_;
f4ca0be2 114 next if /^\#/ || /^\s*$/ || /^\:/;
7ee20c71
NC
115 if ($_ =~ $last) {
116 die "$cfg:$.:section=$section:$_" unless $section == 1;
117 $section = 2;
118 }
f4ca0be2
NC
119 # foo='bar'
120 # foo=bar
58f1e530
NC
121 # (optionally with a trailing comment)
122 if (/^(\w+)=(?:'.*'|[^'].*)(?: #.*)?$/) {
f4ca0be2
NC
123 ++$cfg{$1};
124 } else {
125 warn "$cfg:$.:$_";
126 }
4ccedf19
NC
127 }
128 }
129 close $fh;
130
6692294e 131 ++$test;
e1bac119 132 my $missing;
6692294e
NC
133 if ($cfg eq 'configure.com') {
134 print "ok $test # skip $cfg doesn't need to be sorted\n"
135 if $tap;
136 } elsif (join("", @{$lines[1]}) eq join("", sort @{$lines[1]})) {
137 print "ok $test - $cfg sorted\n"
138 if $tap;
139 } elsif ($tap) {
140 print "not ok $test - $cfg is not sorted\n";
e1bac119
NC
141 } elsif ($opt_r || $opt_l) {
142 # A reference to an empty array is true, hence this flags the
143 # file for later attention by --regen and --list, even if
144 # nothing is missing. Actual sort and output are done later.
145 $missing = [];
146 } else {
147 print "$cfg: unsorted\n"
7ee20c71 148 }
e1bac119 149
1750d3ff 150 for my $v (@MASTER_CFG) {
e1bac119
NC
151 # This only creates a reference in $missing if something is missing:
152 push @$missing, $v unless exists $cfg{$v};
1750d3ff 153 }
e1bac119 154
6692294e 155 ++$test;
e1bac119 156 if ($missing) {
6692294e
NC
157 if ($tap) {
158 print "not ok $test - $cfg missing keys @$missing\n";
159 } elsif ($opt_l) {
e1bac119
NC
160 # print the name once, however many problems
161 print "$cfg\n";
162 } elsif ($opt_r && $cfg ne 'configure.com') {
163 if (defined $default) {
164 push @{$lines[1]}, map {"$_='$default'\n"} @$missing;
165 } else {
166 print "$cfg: missing '$_', use --default to add it\n"
167 foreach @$missing;
168 }
169
170 @{$lines[1]} = sort @{$lines[1]};
171 my $fh = open_new($cfg);
172 print $fh @{$_} foreach @lines;
173 close_and_rename($fh);
174 } else {
175 print "$cfg: missing '$_'\n" foreach @$missing;
176 }
6692294e
NC
177 } elsif ($tap) {
178 print "ok $test - $cfg has no missing keys\n";
7ee20c71 179 }
522c08cc 180}