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