This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
embed.fnc: Mark unlnk as Core only
[perl5.git] / Porting / pod_rules.pl
index 35e26c9..2ba023b 100644 (file)
@@ -1,34 +1,39 @@
 #!/usr/bin/perl -w
 
 use strict;
-use vars qw(%Build %Targets $Verbose $Test);
+our (%Build, %Targets, $Verbose, $Test);
 use Text::Tabs;
 use Text::Wrap;
 use Getopt::Long;
-use Carp;
 
-# Generate the sections of files listed in %Targets from pod.lst
+if (ord("A") == 193) {
+    print "1..0 # EBCDIC sort order is different\n";
+    exit;
+}
+
+# Generate the sections of files listed in %Targets from pod/perl.pod
 # Mostly these are rules in Makefiles
 #
 # --verbose gives slightly more output
 # --build-all tries to build everything
 # --build-foo updates foo as follows
 # --showfiles shows the files to be changed
-# --test exit if perl.pod, pod.lst, MANIFEST are consistent, and regenerated
+# --test exit if perl.pod, MANIFEST are consistent, and regenerated
 #   files are up to date, die otherwise.
 
 %Targets = (
             manifest => 'MANIFEST',
-            perlpod => 'pod/perl.pod',
             vms => 'vms/descrip_mms.template',
             nmake => 'win32/Makefile',
             dmake => 'win32/makefile.mk',
+            gmake => 'win32/GNUmakefile',
             podmak => 'win32/pod.mak',
             unix => 'Makefile.SH',
             # plan9 =>  'plan9/mkfile',
            );
 
-require 'Porting/pod_lib.pl';
+require './Porting/pod_lib.pl';
+require './Porting/manifest_lib.pl';
 sub my_die;
 
 # process command-line switches
@@ -59,54 +64,23 @@ sub my_die;
 }
 
 if ($Verbose) {
-    print "I will be building $_\n" foreach keys %Build;
+    print "I will be building $_\n" foreach sort keys %Build;
 }
 
-my $state = get_pod_metadata(!$Test);
-
 my $test = 1;
-if ($Test) {
-    printf "1..%d\n", 1 + scalar keys %Build;
-    if (@{$state->{inconsistent}}) {
-        print "not ok $test\n";
-        die @{$state->{inconsistent}};
-    }
-    print "ok $test\n";
-}
-else {
-    warn @{$state->{inconsistent}} if @{$state->{inconsistent}};
-}
-
-sub generate_perlpod {
-    my @output;
-    my $maxlength = 0;
-    foreach (@{$state->{master}}) {
-        my $flags = $_->[0];
-        next if $flags->{aux};
-        next if $flags->{perlpod_omit};
-
-        if (@$_ == 2) {
-            # Heading
-            push @output, "=head2 $_->[1]\n";
-        } elsif (@$_ == 5) {
-            # Section
-            my $start = " " x (4 + $flags->{indent}) . $_->[4];
-            $maxlength = length $start if length ($start) > $maxlength;
-            push @output, [$start, $_->[3]];
-        } elsif (@$_ == 0) {
-            # blank line
-            push @output, "\n";
-        } else {
-            my_die "Illegal length " . scalar @$_;
-        }
-    }
-    # want at least 2 spaces padding
-    $maxlength += 2;
-    $maxlength = ($maxlength + 3) & ~3;
-    # sprintf gives $1.....$2 where ... are spaces:
-    return unexpand (map {ref $_ ? sprintf "%-${maxlength}s%s\n", @$_ : $_}
-                     @output);
-}
+# For testing, generated files must be present and we're rebuilding nothing.
+# For normal rebuilding, generated files may not be present, and we mute
+# warnings about inconsistencies in any file we're about to rebuild.
+my $state = $Test
+    ? get_pod_metadata(0, sub {
+                           printf "1..%d\n", 1 + scalar keys %Build;
+                           if (@_) {
+                               print "not ok $test # got Pod metadata\n";
+                               die @_;
+                           }
+                           print "ok $test # got Pod metadata\n";
+                       })
+    : get_pod_metadata(1, sub { warn @_ if @_ }, values %Build);
 
 sub generate_manifest {
     # Annoyingly, unexpand doesn't consider it good form to replace a single
@@ -162,32 +136,26 @@ sub generate_pod_mak {
     $line;
 }
 
-sub verify_contiguous {
-    my ($name, $content, $what) = @_;
-    my $sections = () = $content =~ m/\0+/g;
-    croak("$0: $name contains no $what") if $sections < 1;
-    croak("$0: $name contains discontiguous $what") if $sections > 1;
-}
-
 sub do_manifest {
     my ($name, $prev) = @_;
     my @manifest =
-        grep {! m!^pod/[^.]+\.pod.*!}
+        grep {! m!^pod/[^. \t]+\.pod.*!}
             grep {! m!^README\.(\S+)! || $state->{ignore}{$1}} split "\n", $prev;
-    join "\n", (
-                # Dictionary order - fold and handle non-word chars as nothing
-                map  { $_->[0] }
-                sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] }
-                map  { my $f = lc $_; $f =~ s/[^a-z0-9\s]//g; [ $_, $f ] }
-                @manifest,
-                &generate_manifest_pod(),
-                &generate_manifest_readme()), '';
+    # NOTE - the sort code here is shared with Porting/manisort currently.
+    # If you change one, change the other. Or refactor them. :-)
+    join "\n",  sort_manifest(
+                    @manifest,
+                    &generate_manifest_pod(),
+                    &generate_manifest_readme()
+                ),
+                '', # elegant way to add a newline to the end
+    ;
 }
 
 sub do_nmake {
     my ($name, $makefile) = @_;
-    $makefile =~ s/^\tcopy \.\.\\README.*\n/\0/gm;
-    verify_contiguous($name, $makefile, 'README copies');
+    my $re = qr/^\tcopy \.\.\\README[^\n]*\n/sm;
+    $makefile = verify_contiguous($name, $makefile, $re, 'README copies');
     # Now remove the other copies that follow
     1 while $makefile =~ s/\0\tcopy .*\n/\0/gm;
     $makefile =~ s/\0+/join ("", &generate_nmake_1)/se;
@@ -199,21 +167,7 @@ sub do_nmake {
 
 # shut up used only once warning
 *do_dmake = *do_dmake = \&do_nmake;
-
-sub do_perlpod {
-    my ($name, $pod) = @_;
-
-    unless ($pod =~ s{(For\ ease\ of\ access,\ .*\n)
-                      (?:\s+[a-z]{4,}.*\n   #   fooo
-                      |=head.*\n            # =head foo
-                      |\s*\n                # blank line
-                   )+
-                 }
-                     {$1 . join "", &generate_perlpod}mxe) {
-        my_die "Failed to insert amendments in do_perlpod";
-    }
-    $pod;
-}
+*do_gmake = *do_gmake = \&do_nmake;
 
 sub do_podmak {
     my ($name, $body) = @_;
@@ -231,9 +185,9 @@ sub do_vms {
     # Looking for the macro defining the current perldelta:
     #PERLDELTA_CURRENT = [.pod]perl5139delta.pod
 
-    $makefile =~ s{\nPERLDELTA_CURRENT\s+=\s+\Q[.pod]perl\E\d+delta\.pod\n}
-                  {\0}sx;
-    verify_contiguous($name, $makefile, 'current perldelta macro');
+    my $re = qr{\nPERLDELTA_CURRENT\s+=\s+\Q[.pod]perl\E\d+delta\.pod\n}smx;
+    $makefile
+        = verify_contiguous($name, $makefile, $re, 'current perldelta macro');
     $makefile =~ s/\0+/join "\n", '', "PERLDELTA_CURRENT = [.pod]$state->{delta_target}", ''/se;
 
     $makefile;
@@ -251,15 +205,19 @@ sub do_unix {
     # pod/perl511delta.pod: pod/perldelta.pod
     #         cd pod && $(LNS) perldelta.pod perl511delta.pod
 
-    $makefile_SH =~ s!(
+    # although it seems that HP-UX make gets confused, always tried to
+    # regenerate the symlink, and then the ln -s fails, as the target exists.
+
+    my $re = qr{(
 pod/perl[a-z0-9_]+\.pod: pod/perl[a-z0-9_]+\.pod
+       \$\(RMS\) pod/perl[a-z0-9_]+\.pod
        \$\(LNS\) perl[a-z0-9_]+\.pod pod/perl[a-z0-9_]+\.pod
-)+!\0!gm;
-
-    verify_contiguous($name, $makefile_SH, 'copy rules');
+)+}sm;
+    $makefile_SH = verify_contiguous($name, $makefile_SH, $re, 'copy rules');
 
     my @copy_rules = map "
 pod/$_: pod/$state->{copies}{$_}
+       \$(RMS) pod/$_
        \$(LNS) $state->{copies}{$_} pod/$_
 ", keys %{$state->{copies}};
 
@@ -268,44 +226,7 @@ pod/$_: pod/$state->{copies}{$_}
 }
 
 # Do stuff
-while (my ($target, $name) = each %Targets) {
-    print "Now processing $name\n" if $Verbose;
-
-    my $fh = open_or_die($name);
-    binmode $fh;
-    local $/;
-    my $orig = <$fh>;
-    my_die "$name contains NUL bytes" if $orig =~ /\0/;
-
-    my $new = do {
-        no strict 'refs';
-        &{"do_$target"}($target, $orig);
-    };
-
-    if ($Test) {
-        printf "%s %d # $name is up to date\n",
-            $new eq $orig ? 'ok' : 'not ok',
-                ++$test;
-        next;
-    } elsif ($new eq $orig) {
-        print "Was not modified\n"
-            if $Verbose;
-        next;
-    }
-
-    my $mode = (stat $name)[2] // my_die "Can't stat $name: $!";
-    rename $name, "$name.old" or my_die "Can't rename $name to $name.old: $!";
+process($_, $Build{$_}, main->can("do_$_"), $Test && ++$test, $Verbose)
+    foreach sort keys %Build;
 
-    open $fh, '>', $name or my_die "Can't open $name for writing: $!";
-    binmode $fh;
-    print $fh $new or my_die "print to $name failed: $!";
-    close $fh or my_die "close $name failed: $!";
-    chmod $mode & 0777, $name or my_die "can't chmod $mode $name: $!";
-}
-
-# Local variables:
-# cperl-indent-level: 4
-# indent-tabs-mode: nil
-# End:
-#
 # ex: set ts=8 sts=4 sw=4 et: