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