This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make spelling of values for 'FILES' consistent
[perl5.git] / Porting / pod_rules.pl
CommitLineData
b78c1104
NC
1#!/usr/bin/perl -w
2
3use strict;
3d3a0a8a 4our (%Build, %Targets, $Verbose, $Test);
b78c1104
NC
5use Text::Tabs;
6use Text::Wrap;
7use Getopt::Long;
b78c1104 8
ff23b6f8
KW
9if (ord("A") == 193) {
10 print "1..0 # EBCDIC sort order is different\n";
11 exit;
12}
13
0aef0fe5 14# Generate the sections of files listed in %Targets from pod/perl.pod
b78c1104
NC
15# Mostly these are rules in Makefiles
16#
17# --verbose gives slightly more output
18# --build-all tries to build everything
19# --build-foo updates foo as follows
20# --showfiles shows the files to be changed
9f9c5d81 21# --tap emit TAP (testing) output describing the state of the pod files
b78c1104
NC
22
23%Targets = (
24 manifest => 'MANIFEST',
b78c1104
NC
25 vms => 'vms/descrip_mms.template',
26 nmake => 'win32/Makefile',
1599518e 27 gmake => 'win32/GNUmakefile',
b78c1104
NC
28 podmak => 'win32/pod.mak',
29 unix => 'Makefile.SH',
30 # plan9 => 'plan9/mkfile',
31 );
32
3d7c117d
MB
33require './Porting/pod_lib.pl';
34require './Porting/manifest_lib.pl';
b78c1104
NC
35sub my_die;
36
37# process command-line switches
38{
39 my @files = keys %Targets;
40 my $filesopts = join(" | ", map { "--build-$_" } "all", sort @files);
41 my $showfiles;
42 my %build_these;
43 die "$0: Usage: $0 [--verbose] [--showfiles] [$filesopts]\n"
44 unless GetOptions (verbose => \$Verbose,
45 showfiles => \$showfiles,
46 tap => \$Test,
47 map {+"build-$_", \$build_these{$_}} @files, 'all')
48 && !@ARGV;
49 if ($build_these{all}) {
50 %Build = %Targets;
51 } else {
52 while (my ($file, $want) = each %build_these) {
53 $Build{$file} = $Targets{$file} if $want;
54 }
55 # Default to --build-all if no targets given.
56 %Build = %Targets if !%Build;
57 }
58 if ($showfiles) {
59 print join(" ", sort { lc $a cmp lc $b } values %Build), "\n";
60 exit(0);
61 }
62}
63
64if ($Verbose) {
23645649 65 print "I will be building $_\n" foreach sort keys %Build;
b78c1104
NC
66}
67
d4c6b7ae 68my $test = 1;
9e241592
NC
69# For testing, generated files must be present and we're rebuilding nothing.
70# For normal rebuilding, generated files may not be present, and we mute
71# warnings about inconsistencies in any file we're about to rebuild.
d4c6b7ae
NC
72my $state = $Test
73 ? get_pod_metadata(0, sub {
74 printf "1..%d\n", 1 + scalar keys %Build;
75 if (@_) {
23645649 76 print "not ok $test # got Pod metadata\n";
d4c6b7ae
NC
77 die @_;
78 }
23645649 79 print "ok $test # got Pod metadata\n";
d4c6b7ae
NC
80 })
81 : get_pod_metadata(1, sub { warn @_ if @_ }, values %Build);
b78c1104 82
b78c1104
NC
83sub generate_manifest {
84 # Annoyingly, unexpand doesn't consider it good form to replace a single
85 # space before a tab with a tab
86 # Annoyingly (2) it returns read only values.
87 my @temp = unexpand (map {sprintf "%-32s%s", @$_} @_);
88 map {s/ \t/\t\t/g; $_} @temp;
89}
90
91sub generate_manifest_pod {
92 generate_manifest map {["pod/$_.pod", $state->{pods}{$_}]}
93 sort grep {
94 !$state->{copies}{"$_.pod"}
95 && !$state->{generated}{"$_.pod"}
96 && !-e "$_.pod"
97 } keys %{$state->{pods}};
98}
99
100sub generate_manifest_readme {
101 generate_manifest sort {$a->[0] cmp $b->[0]}
102 ["README.vms", "Notes about installing the VMS port"],
103 map {["README.$_", $state->{readmes}{$_}]} keys %{$state->{readmes}};
104}
105
106sub generate_nmake_1 {
107 # XXX Fix this with File::Spec
108 (map {sprintf "\tcopy ..\\README.%-8s ..\\pod\\perl$_.pod\n", $_}
109 sort keys %{$state->{readmes}}),
110 (map {"\tcopy ..\\pod\\$state->{copies}{$_} ..\\pod\\$_\n"}
111 sort keys %{$state->{copies}});
112}
113
114# This doesn't have a trailing newline
115sub generate_nmake_2 {
116 # Spot the special case
117 local $Text::Wrap::columns = 76;
118 my $line = wrap ("\t ", "\t ",
119 join " ", sort(keys %{$state->{copies}},
120 keys %{$state->{generated}},
121 map {"perl$_.pod"} keys %{$state->{readmes}}));
122 $line =~ s/$/ \\/mg;
123 $line =~ s/ \\$//;
124 $line;
125}
126
127sub generate_pod_mak {
128 my $variable = shift;
129 my @lines;
130 my $line = "\U$variable = " . join "\t\\\n\t",
131 map {"$_.$variable"} sort grep { $_ !~ m{/} } keys %{$state->{pods}};
132 # Special case
133 $line =~ s/.*perltoc.html.*\n//m;
134 $line;
135}
136
b78c1104
NC
137sub do_manifest {
138 my ($name, $prev) = @_;
139 my @manifest =
0aef0fe5 140 grep {! m!^pod/[^. \t]+\.pod.*!}
b78c1104 141 grep {! m!^README\.(\S+)! || $state->{ignore}{$1}} split "\n", $prev;
3dfcef7e
YO
142 # NOTE - the sort code here is shared with Porting/manisort currently.
143 # If you change one, change the other. Or refactor them. :-)
144 join "\n", sort_manifest(
145 @manifest,
146 &generate_manifest_pod(),
147 &generate_manifest_readme()
148 ),
149 '', # elegant way to add a newline to the end
150 ;
b78c1104
NC
151}
152
153sub do_nmake {
154 my ($name, $makefile) = @_;
8f55fd21
NC
155 my $re = qr/^\tcopy \.\.\\README[^\n]*\n/sm;
156 $makefile = verify_contiguous($name, $makefile, $re, 'README copies');
b78c1104
NC
157 # Now remove the other copies that follow
158 1 while $makefile =~ s/\0\tcopy .*\n/\0/gm;
159 $makefile =~ s/\0+/join ("", &generate_nmake_1)/se;
160
161 $makefile =~ s{(-cd \$\(PODDIR\) && del /f[^\n]+).*?(-cd \.\.\\utils && del /f)}
162 {"$1\n" . &generate_nmake_2."\n\t$2"}se;
163 $makefile;
164}
165
166# shut up used only once warning
1599518e 167*do_gmake = *do_gmake = \&do_nmake;
b78c1104 168
b78c1104
NC
169sub do_podmak {
170 my ($name, $body) = @_;
171 foreach my $variable (qw(pod man html tex)) {
172 my_die "could not find $variable in $name"
173 unless $body =~ s{\n\U$variable\E = (?:[^\n]*\\\n)*[^\n]*}
174 {"\n" . generate_pod_mak ($variable)}se;
175 }
176 $body;
177}
178
179sub do_vms {
180 my ($name, $makefile) = @_;
181
182 # Looking for the macro defining the current perldelta:
183 #PERLDELTA_CURRENT = [.pod]perl5139delta.pod
184
8f55fd21
NC
185 my $re = qr{\nPERLDELTA_CURRENT\s+=\s+\Q[.pod]perl\E\d+delta\.pod\n}smx;
186 $makefile
187 = verify_contiguous($name, $makefile, $re, 'current perldelta macro');
b78c1104
NC
188 $makefile =~ s/\0+/join "\n", '', "PERLDELTA_CURRENT = [.pod]$state->{delta_target}", ''/se;
189
190 $makefile;
191}
192
193sub do_unix {
194 my ($name, $makefile_SH) = @_;
195
196 $makefile_SH =~ s{^(perltoc_pod_prereqs = extra.pods).*}
197 {join ' ', $1, map "pod/$_",
198 sort(keys %{$state->{copies}},
199 grep {!/perltoc/} keys %{$state->{generated}})
200 }mge;
201
202 # pod/perl511delta.pod: pod/perldelta.pod
203 # cd pod && $(LNS) perldelta.pod perl511delta.pod
204
9fa1f09f
NC
205 # although it seems that HP-UX make gets confused, always tried to
206 # regenerate the symlink, and then the ln -s fails, as the target exists.
207
8f55fd21 208 my $re = qr{(
b78c1104 209pod/perl[a-z0-9_]+\.pod: pod/perl[a-z0-9_]+\.pod
9fa1f09f 210 \$\(RMS\) pod/perl[a-z0-9_]+\.pod
b78c1104 211 \$\(LNS\) perl[a-z0-9_]+\.pod pod/perl[a-z0-9_]+\.pod
8f55fd21
NC
212)+}sm;
213 $makefile_SH = verify_contiguous($name, $makefile_SH, $re, 'copy rules');
b78c1104
NC
214
215 my @copy_rules = map "
216pod/$_: pod/$state->{copies}{$_}
9fa1f09f 217 \$(RMS) pod/$_
b78c1104
NC
218 \$(LNS) $state->{copies}{$_} pod/$_
219", keys %{$state->{copies}};
220
221 $makefile_SH =~ s/\0+/join '', @copy_rules/se;
222 $makefile_SH;
223}
224
a2b7b21f 225# Do stuff
23645649
NC
226process($_, $Build{$_}, main->can("do_$_"), $Test && ++$test, $Verbose)
227 foreach sort keys %Build;
a2b7b21f 228
b78c1104 229# ex: set ts=8 sts=4 sw=4 et: