This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Silence warnings when -destdir isn't set.
[perl5.git] / regen_lib.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use vars qw($Is_W32 $Is_OS2 $Is_Cygwin $Is_NetWare $Needs_Write $Verbose
4             @Changed);
5 use Config; # Remember, this is running using an existing perl
6 use File::Compare;
7 use Symbol;
8
9 # Common functions needed by the regen scripts
10
11 $Is_W32 = $^O eq 'MSWin32';
12 $Is_OS2 = $^O eq 'os2';
13 $Is_Cygwin = $^O eq 'cygwin';
14 $Is_NetWare = $Config{osname} eq 'NetWare';
15 if ($Is_NetWare) {
16   $Is_W32 = 0;
17 }
18
19 $Needs_Write = $Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_NetWare;
20
21 $Verbose = 0;
22 @ARGV = grep { not($_ eq '-q' and $Verbose = -1) }
23   grep { not($_ eq '-v' and $Verbose = 1) } @ARGV;
24
25 END {
26   print STDOUT "Changed: @Changed\n" if @Changed;
27 }
28
29 sub safer_unlink {
30   my @names = @_;
31   my $cnt = 0;
32
33   my $name;
34   foreach $name (@names) {
35     next unless -e $name;
36     chmod 0777, $name if $Needs_Write;
37     ( CORE::unlink($name) and ++$cnt
38       or warn "Couldn't unlink $name: $!\n" );
39   }
40   return $cnt;
41 }
42
43 sub safer_rename_silent {
44   my ($from, $to) = @_;
45
46   # Some dosish systems can't rename over an existing file:
47   safer_unlink $to;
48   chmod 0600, $from if $Needs_Write;
49   rename $from, $to;
50 }
51
52 sub rename_if_different {
53   my ($from, $to) = @_;
54
55   if (compare($from, $to) == 0) {
56       warn "no changes between '$from' & '$to'\n" if $Verbose > 0;
57       safer_unlink($from);
58       return;
59   }
60   warn "changed '$from' to '$to'\n" if $Verbose > 0;
61   push @Changed, $to unless $Verbose < 0;
62   safer_rename_silent($from, $to) or die "renaming $from to $to: $!";
63 }
64
65 # Saf*er*, but not totally safe. And assumes always open for output.
66 sub safer_open {
67     my $name = shift;
68     my $fh = gensym;
69     open $fh, ">$name" or die "Can't create $name: $!";
70     *{$fh}->{SCALAR} = $name;
71     binmode $fh;
72     $fh;
73 }
74
75 sub safer_close {
76     my $fh = shift;
77     close $fh or die 'Error closing ' . *{$fh}->{SCALAR} . ": $!";
78 }
79
80 1;