This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regexp_unicode_prop.t: Generalize for non-ASCII platforms
[perl5.git] / Porting / pod_rules.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw(%Build %Targets $Verbose $Test);
5 use Text::Tabs;
6 use Text::Wrap;
7 use Getopt::Long;
8
9 if (ord("A") == 193) {
10     print "1..0 # EBCDIC sort order is different\n";
11     exit;
12 }
13
14 # Generate the sections of files listed in %Targets from pod/perl.pod
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
21 # --test exit if perl.pod, MANIFEST are consistent, and regenerated
22 #   files are up to date, die otherwise.
23
24 %Targets = (
25             manifest => 'MANIFEST',
26             vms => 'vms/descrip_mms.template',
27             nmake => 'win32/Makefile',
28             dmake => 'win32/makefile.mk',
29             podmak => 'win32/pod.mak',
30             unix => 'Makefile.SH',
31             # plan9 =>  'plan9/mkfile',
32            );
33
34 require 'Porting/pod_lib.pl';
35 sub 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
64 if ($Verbose) {
65     print "I will be building $_\n" foreach sort keys %Build;
66 }
67
68 my $test = 1;
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.
72 my $state = $Test
73     ? get_pod_metadata(0, sub {
74                            printf "1..%d\n", 1 + scalar keys %Build;
75                            if (@_) {
76                                print "not ok $test # got Pod metadata\n";
77                                die @_;
78                            }
79                            print "ok $test # got Pod metadata\n";
80                        })
81     : get_pod_metadata(1, sub { warn @_ if @_ }, values %Build);
82
83 sub 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
91 sub 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
100 sub 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
106 sub 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
115 sub 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
127 sub 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
137 sub do_manifest {
138     my ($name, $prev) = @_;
139     my @manifest =
140         grep {! m!^pod/[^. \t]+\.pod.*!}
141             grep {! m!^README\.(\S+)! || $state->{ignore}{$1}} split "\n", $prev;
142     join "\n", (
143                 # Dictionary order - fold and handle non-word chars as nothing
144                 map  { $_->[0] }
145                 sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] }
146                 map  { my $f = lc $_; $f =~ s/[^a-z0-9\s]//g; [ $_, $f ] }
147                 @manifest,
148                 &generate_manifest_pod(),
149                 &generate_manifest_readme()), '';
150 }
151
152 sub do_nmake {
153     my ($name, $makefile) = @_;
154     my $re = qr/^\tcopy \.\.\\README[^\n]*\n/sm;
155     $makefile = verify_contiguous($name, $makefile, $re, 'README copies');
156     # Now remove the other copies that follow
157     1 while $makefile =~ s/\0\tcopy .*\n/\0/gm;
158     $makefile =~ s/\0+/join ("", &generate_nmake_1)/se;
159
160     $makefile =~ s{(-cd \$\(PODDIR\) && del /f[^\n]+).*?(-cd \.\.\\utils && del /f)}
161                   {"$1\n" . &generate_nmake_2."\n\t$2"}se;
162     $makefile;
163 }
164
165 # shut up used only once warning
166 *do_dmake = *do_dmake = \&do_nmake;
167
168 sub do_podmak {
169     my ($name, $body) = @_;
170     foreach my $variable (qw(pod man html tex)) {
171         my_die "could not find $variable in $name"
172             unless $body =~ s{\n\U$variable\E = (?:[^\n]*\\\n)*[^\n]*}
173                              {"\n" . generate_pod_mak ($variable)}se;
174     }
175     $body;
176 }
177
178 sub do_vms {
179     my ($name, $makefile) = @_;
180
181     # Looking for the macro defining the current perldelta:
182     #PERLDELTA_CURRENT = [.pod]perl5139delta.pod
183
184     my $re = qr{\nPERLDELTA_CURRENT\s+=\s+\Q[.pod]perl\E\d+delta\.pod\n}smx;
185     $makefile
186         = verify_contiguous($name, $makefile, $re, 'current perldelta macro');
187     $makefile =~ s/\0+/join "\n", '', "PERLDELTA_CURRENT = [.pod]$state->{delta_target}", ''/se;
188
189     $makefile;
190 }
191
192 sub do_unix {
193     my ($name, $makefile_SH) = @_;
194
195     $makefile_SH =~ s{^(perltoc_pod_prereqs = extra.pods).*}
196                      {join ' ', $1, map "pod/$_",
197                           sort(keys %{$state->{copies}},
198                                grep {!/perltoc/} keys %{$state->{generated}})
199                       }mge;
200
201     # pod/perl511delta.pod: pod/perldelta.pod
202     #         cd pod && $(LNS) perldelta.pod perl511delta.pod
203
204     # although it seems that HP-UX make gets confused, always tried to
205     # regenerate the symlink, and then the ln -s fails, as the target exists.
206
207     my $re = qr{(
208 pod/perl[a-z0-9_]+\.pod: pod/perl[a-z0-9_]+\.pod
209         \$\(RMS\) pod/perl[a-z0-9_]+\.pod
210         \$\(LNS\) perl[a-z0-9_]+\.pod pod/perl[a-z0-9_]+\.pod
211 )+}sm;
212     $makefile_SH = verify_contiguous($name, $makefile_SH, $re, 'copy rules');
213
214     my @copy_rules = map "
215 pod/$_: pod/$state->{copies}{$_}
216         \$(RMS) pod/$_
217         \$(LNS) $state->{copies}{$_} pod/$_
218 ", keys %{$state->{copies}};
219
220     $makefile_SH =~ s/\0+/join '', @copy_rules/se;
221     $makefile_SH;
222 }
223
224 # Do stuff
225 process($_, $Build{$_}, main->can("do_$_"), $Test && ++$test, $Verbose)
226     foreach sort keys %Build;
227
228 # Local variables:
229 # cperl-indent-level: 4
230 # indent-tabs-mode: nil
231 # End:
232 #
233 # ex: set ts=8 sts=4 sw=4 et: